‘Pages’ In Menu Or Sidebar

How to get your Pages to appear in either the top Menu or the Sidebar

Note 1: I’m using WordPress 2.1 and the K2 Theme

Note 2: The line numbers apply to the K2 Theme (095rc1) 

Note 3: Some of the code formatting is a little weird, but you’ll get the idea.

I ran into a situation that’s easy to fix, and wanted to share a solution.

I really like the K2 Theme, but one thing I noticed was that every Page I created only appeared in the top Menu. That’s the way the K2 guys made it work, and that’s cool, but things get weird if you have more than 4 or 5 Pages, especially if the Page names are more than 1 word.

So I got into the code to see if I could split things up and send certain Pages to the top Menu, and others to the Sidebar. It’s a pretty easy fix, and can be done with the ‘exclude’ argument and a few extra lines of code.

Here’s how it works:

Step 1

Open your administration panel and make a note of the ID associated with each page you have. (That’s in Manage > Pages just to be clear). Determine which pages you want to be visible in the Menu, and which you want in the Sidebar.

Step 2

Open wp-content/themes/K2/header.php

On line 86, change

<?php wp_list_pages(’sort_column=menu_order & depth=1 & title_li=’); ?>

to

<?php wp_list_pages(‘exclude=8 & sort_column=menu_order & depth=1 & title_li=’); ?>

By using the exclude=* argument (in my case, the Page with an ID of 8), that Page will not appear in the top Menu.

Note: If you want to exclude multiple pages this way, seperate the numbers with commas, no spaces, and in numerical order, like this: exclude=1,4,6,9 not exclude=4,1,9,6. See the Codex for more specifics.

That’s cool, but I do want that Page to appear somewhere, and I want it to be the Sidebar. So let’s add some code to make it do that.

Step 3

Open wp-content/themes/K2/sidebar.php

Around line 140 or so, add

<?php /* Pages */ if ( (is_home()) or (is_search() or (is_404()) or ($notfound == ‘1′)) or (function_exists(‘is_tag’) and is_tag()) or ( (is_archive()) and (!is_author()) ) ) { ?>

 <div class=”sb-latest”>
  <h2><?php _e(‘Pages’,'k2_domain’); ?></h2>

    <ul>
     <?php wp_list_pages(‘exclude=2 & sort_column=menu_order & depth=1 & title_li=’); ?>
    </ul>

 </div>
 <?php } ?>

I chose this location because I wanted the Pages that aren’t visible in the top Menu to be visible under the ‘Latest Entries’ (posts) in the Sidebar, and I just copied the code-block for ‘Latest Entries’ (line 128) and pasted it underneath itself. Then I modified part of the new code-block, which originally looked like this:

  <ul>
   <?php wp_get_archives(‘type=postbypost & limit=10′); ?>
  </ul>

and made it look like this:

  <ul>
   <?php wp_list_pages(‘exclude=2 & sort_column=menu_order & depth=1 & title_li=’); ?>
  </ul>

I changed wp_get_archives to wp_list_pages, and again, I used the exclude=* argument (in my case, the Page with an ID of 2) to show the Page that was not visible in the top Menu, and make it visible in the Sidebar. (The other arguments can be changed to suit you, see the Codex for details). The original code-block for ‘Latest Entries’ also contained a <span> tag with some additional code for an RSS feed, but I left that out of my new ‘Pages’ code-block because I didn’t need that functionality. I also used the same CSS class as ‘Latest’ for my new ‘Pages’, but you could write a new CSS class if you want it to look different.

That’s pretty much it.

This example works, but it has the potential to get weird if you have a lot of pages. Personally, I like pages, and plan to use a lot of them in my next project. So my next experiment is to get a checkbox or something in the administration panel that will allow me to select a destination for each new page (i.e., Menu or Sidebar, maybe both). But it’s still early in the game, so I might discover a better solution (there may even be an existing plug-in out there already; I haven’t even looked). Either way, I’ll post the result here when I have one.

5 Responses to “‘Pages’ In Menu Or Sidebar”


  1. 1 Mohammed Zainal February 10, 2007 at 3:43 am

    i see someone has been busy ;)
    good post buddy .

  2. 2 mank1327 February 11, 2007 at 9:52 am

    Hey, man. How’s it going?

    Yeah, I had been working with WP 2.1/K2 on my localhost, but I switched over to Smarty to get a basic understanding of how that works. That only lasted for a few days before I started playing with Scriptaculous and jQuery, and most recently Code Igniter.

    If you’re interested in PEAR, definitely check out Code Igniter. It’s lightweight, the documentation is very good, and the forum members are helpful and friendly.

    Hasin’s book arrived here last week, so I took a day off and laid on the couch and read the whole thing. The first half is devoted to the basics, but everything after chapter 5 is about more advanced stuff; modifying and building your own themes, plugins and widgets, etc. The concepts can be applied elsewhere, and I’m glad I bought it.

    Good to hear from you, man. Later!

  3. 3 Mohammed Zainal March 19, 2007 at 5:20 am

    hey hey thanks for the info …. but it seems like that you haven’t posted Any X long time ago … whats up ?

  4. 4 mank1327 March 20, 2007 at 2:14 pm

    I got neck-deep in Code Igniter.

    It started out as a little project to learn the framework, and the next thing I knew I was building a CMS. It’s coming along well, and I expect to have the first version up on a real domain by April 1st. I’ll send you a link.

    Hey, I checked out your ver2 site. It looks like that’ll be cool. Then again, the current version is pretty sweet too.

    Hope everything is well in Bahrain. Arizona finally got warm enough for me. I wish we had ocean water here though. Good thing we got a pool!

    Dude, have you seen the next-gen Camaro? Take a look at the Chevy site if you haven’t. There’s some nice pics and stuff. http://www.chevrolet.com/performance/

    Later!

  5. 5 Mohammed Zainal June 9, 2007 at 12:21 pm

    oh yah i did … i want one !!

    well good luck with the CMS mate … BTW i moved my blog , u may wanna edit the blogroll ;)


Leave a Reply