Subscribe!
RSS
  • Moshu
  • Prairie
  • WordPress
  • The combined blog: about WordPress, the Canadian Prairies and myself
    The Journey to full time blogging


Creating a two level menu with Pages - part 2

Welcome back! I hope you enjoyed your previous visits. Let me know how can I help you to find what you were looking for. Don't forget to download the Best 7 WP Tutorials in this ebook.

A while back I promised to write the second part of the Two level menus article. Today Susan reminded me about it, so here we go: Part 2 - styling and highlighting the two level menu.

In the examples I will refer to the same demo installation as I did in Part 1. (You can right click the link and open it in another tab to be able to follow the explanation.

You remember, we created a top horizontal menu using the Fold Page plugin and using this code:

<?php wswwpx_fold_page_list ('depth=1&sort_column=menu_order&title_li='); ?>

Eventually, on the Demo site I modified this to include a “Home” link and to have one of the categories also included (as explained in another post about Pages and Categories).
This is the modified code used on the demo:

<ul>
<li id="one"><a href="<?php bloginfo('home'); ?>">Home</a></li>
<?php wswwpx_fold_page_list ('depth=1&sort_column=menu_order&title_li='); ?>
<li id="five"><a href="<?php bloginfo('url'); ?>/?cat=18">Hidden</a></li>
</ul>

You can notice I have added ids to the first and last li items. Let me explain it but before that comes the disclaimer: since I am not a real coder my solution is not perfect and is not really dynamic; more knowledgeable coders, probably, could come up with a more elegant solution. However, it worked for me. The explanation:
I have five items in my horizontal menu: Home, About, Archives, Contact, Hidden. I know it will not change, so I can put this code in the head section of the header file:

<?php
if ( is_home() ) { $current = 'one'; }
elseif ( is_page('2') ) { $current = 'two'; } //id of the About page
elseif ( is_page('6') ) { $current = 'three'; } //id of the Archive page
elseif ( is_page('16') ) { $current = 'four'; } //id of the Contact page
elseif(get_the_title($post->post_parent) != the_title(' ' , ' ',false)) { $current = 'post_parent'; }
elseif ( is_category('18') ) { $current = 'five'; }//my hidden cat
?>

Now we are prepared to style our menu. The code above will set the “current” class depending on which page/Page we are on. For the styling we also have to remember that the Fold Page plugin adds a special class to the parent Page when we are on the children Pages. You can check the source code on the demo: when we are on the sub-Pages of About the li item around this parent will get the “current_page_ancestor” class (pretty much self-explanatory…).

Now will add a bit of CSS into our head section:

<style type="text/css">
#<?php echo $current; ?> {
background-color: #ff0000;
padding: 0 16px;
}
#nav .current_page_item {
background-color: #6eaae9;
padding: 3px 8px 2px 8px;
}
.current_page_item, .current_page_ancestor {
background-color: #ff0000;
padding: 0 16px;
}
</style>

A few quick notes to this: lines #2-5 set the background colour for the “current” page/Page.
Lines #6-9 set the background for the sub-Pages in the sidebar - my sidebar is in a div id=”nav”!
And, finally, the last lines will set the colour for the “ancestor Page (in the case of the Demo - About) and for the current page items in the top menu.
Of course, you can use all your colours and style elements that fit into your theme and make it part of your design.

In case you want to see the whole header.php used in demo - here is the whole code.

Basically, that’s all. As I said, it’s more like a hack than elegant code but it works well with a horizontal menu that is not going to change too much in the future. When I needed a quick fix for a blog with a similar menu system, I found the components for this solution in the Codex and in the WP support forums… I just put them together.
If you have suggestions to improve it, to make it more dynamic, feel free to take it and make it better. And if you just need a quick fix - enjoy it.


13 Responses

  • Susan ·

    Thank you so much for taking the time to do this! I know you’ve been busy, but I really appreciate it.

  • Blogging Squared ·

    Thanks for taking time to write the second part of this article. It helped solve a WordPress problem that we were having with a clients web site.

  • Moshu ·

    I am glad you found it helpful. As a sidenote: didn’t find anything at this link on your site: http://bloggingsquared.com/book/
    Would like to see it if it’s possible…

  • Melissa ·

    Thank you so much for the great codes! You helped me solve my Wordpress problem. Just when I thought maybe I should give up creating my own layout, and then I found this post and it really helped me a lot.

  • Stephanie ·

    Oh wow I just love blogs like this. I consider myself a new user when it comes to blogging even though I have been blogging for quite some time now. I have yet to get into the formatting and coding and all that complicated stuff but thanks to blogs like this I might just do that.

  • Moshu ·

    Stephanie, that sounds nice - except that on wordpress.COM where your blog is, you cannot use any of these tutorials: they are exclusively for self-hosted WP blogs.

  • mini me ·

    The demo site seems to be down. Any chance you can put it back up? BTW thanks for this resource.

  • Moshu ·

    Sorry about that.
    It is back now (actually, it was there but under a slightly different url).
    It should work now:
    http://www.transycan.net/devdemo

  • mini me ·

    Splendid!!!! This has helped me more than you care to know :D

  • Jan Ligaard ·

    Hi

    Thanks for you great tutorial. It saved my day, now I just need to tweek :-)

    One question: I have made a page called “Blog” and directed the frontpage for the post to this menuitem. So when I press blog I get the overview and the “Blog” in the menu is highlighted. The problem is when I select a post in the blog list, the highlight on the “blog” dissapears. Any idea on how I can keep the “blog” menu item highlighted when I am on blog pages?

    Take a look here: http://www.icon22.com/test

    Best

    Jan

  • Moshu ·

    Hi Jan,

    You should change the conditional tags (if statements) according to this:
    http://codex.wordpress.org/Conditional_Tags#The_Main_Page

    Since the introduction of the frontpage and postpage options in Readings - you have to change the if code accordingly.

    Hope this helps.

  • Jan Ligaard ·

    Hi Moshu

    Yea that helped me understand way, but since I am not a coder, I have a bit of trouble finding out where in the code I shall make the changes.

    Can you point me in a direction.

    Thanks in advance.

    Jan

1 Trackbacks/Pingbacks

  1. Playing with WordPress » Creating a two level menu with Pages - part 1

RSS feed for comments on this post.   TrackBack URI

Leave a Reply

Widgetize Anything
in Your WordPress Blog

Have you ever dreamed about
putting widgets everywhere in your theme?

Get Widget Magic

NOW YOU CAN! (click here)

Download the WIDGET MAGIC
and follow the easy steps!


You are just one click away from widgetizing anything in your blog...



  • Make and save money!
Hassle-free blog setup in minutes   Click here to join Easy Video Producer
AvoidForeclosureHell   Text Link Ads