Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Talk:Dynamic Menu Highlighting

Easier Way

There is an easier way in selecting the current link using PHP.

If you change the body id dinamically, you can change the links with CSS this way:

 body#home a.home,
 body#page a.page,
 body#anything a.anything {
   font-weight: bolder;
   color: yellow;
 }

And you can keep the menu without PHP, as simple as this example:

 <ul class="menu">
   <li><a href="/" class="home">Home</a></li>
   <li><a href="/page/" class="page">Home</a></li>
   <li><a href="/anything/" class="anything">Home</a></li>
 </ul>

See? No need to put "current" on each link. Let the CSS do the work for you. --Alenonimo 09:09, 27 Aug 2007 (UTC)


Template Tags/wp_list_pages styles the current page link with an extra class: current_page_item. --MDAWaffe - Talk (forgot to sign, sorry!)


This is great, but it looks like this "current page" issue turned up in a wp_list_pages. Can we include this information AND how to stylize this same technique for categories, since that is what is usually the most requested. Not the Pages feature so much as what category the post is in. A kind of breadcrumb effect. Will this technique work for that?

This is excellent information. Thanks for putting it together! Lorelle 16:14, 17 Mar 2005 (UTC)


I'll have to research a little more and see what I can do. I'm sure it'll work, but I don't exactly know how at the moment. I'll work on expanding it to do categories. Anyone else is welcome to help me.

Well, with just a little reasearch, it looks like this won't work with categories; at least not with category lists built with list_cats because I couldn't see a way to customize the list items it outputs to include the unique id's. --Avenir 16:58, 17 Mar 2005 (UTC)

ARGHHHH! Let's keep poking and prodding. Squeaky wheel and all that. This is beautiful and I intend on slashing (hardly at all) on it later today when I get a chance. Wonderful stuff. Lorelle 17:15, 17 Mar 2005 (UTC)

Can't this be done with Template Tags/wp_list_pages#CSS Styles? (see first comment on this discussion page). Perhaps I am missing something? --MDAWaffe - Talk 10:15, 24 Mar 2005 (UTC)

DUH! You're right. I thought there was an example with the tag in this article. I look closer and there isn't. I'll clean this up on Wiki Weekend so we include that as an example. I'm going to add graphics to this, too. Good catch. Lorelle 23:02, 24 Mar 2005 (UTC)

UPDATE Q1 2006

Just changed the begining of the article, added a pic. I think this article is in essence quite ok, I would like to simply clean it up a bit and add references within the code/examples themselves. Of course, the modifications from the discussion above should also be included where appropriate. Let me know if you don't think that is ok. Ptryk 18:42, 2 Feb 2006 (GMT)

Update: Beginning of August 2006

I added a second way of going about all this, one that lets all the CSS stay in one place: the CSS document. This is the method I use on my site and it allows for a bit more than the previous method.

This has seemed to focus on creating a dynamic menu when working with Pages, but I tried to bring in how to make a blog menu dynamic as well. I think this should focus on the dynamic menu creation and not the specific application to Pages.

I tried to keep the working example simple, but then dropped a big chunk of code at the end that has a few different scenarios in it, allowing visitors to see some possibilities. What needs to change? Did I just complicate things too much? Should we have two pages, one that shows how to implement these menus for a site using Pages and the other for a site using the regular blog features of WP? Todd 16:42, 12 Aug 2006 (GMT)