Removing current_page_parent from wp_list_pages from search and 404 pages

on February 10th, 2010 by Richard Orelup in Development - Wordpress
| Share

Yesterday got an IM from my buddy Atom Groom about an issue he was having with a WordPress project he was working on. I’d personally never run into the issue before and it didn’t really make sense when he was first explaining it. I setup a test instance and finally realized what was going on.

When using the wp_list_pages() function to generate your navigation it will add a class of “current_page_item” to the current page in that list. As well when you are on a child of another page the parent page will get a class of “current_page_parent”. Now this is great overall because with little work you can easily setup a nice navigation system.

So what’s the problem? By default the search and 404 pages have the main posts page set as their parent. So now when you’re on one of those pages your navigation show that as the parent page of this one. There is no easy way in the admin side to stop this or to set what the parent would be for those cases (if anyone knows of a way please let me know as I obviously missed it.) So went ahead did the normal thing you have to do when you want to remove those strange default things WordPress does automatically, used a filter on the wp_list_pages() function. Code is below (Link to Gist if not showing up) –

What’s strange is I couldn’t easily find any information on this issue. I couldn’t believe that more people hadn’t run into this issue before and that it would be talked about. Then it hit me why this probably isn’t an issue, people more often then not probably don’t use the static page and posts page. As well, when doing a standard blog, most handle the “home” in their navigation separate from the rest so it wasn’t being auto generated (or don’t even have a home link in the nav because they expect you to click the header.)

With WordPress becoming more of a CMS I think people will run into this more and more. So I figured why not write a post and hope that this can help someone out there with the same info who might not be finding anything.

So enjoy, if you have any questions please ask.

Comments: 10 Tags:

10 Responses to “Removing current_page_parent from wp_list_pages from search and 404 pages”

  1. Atom Groom says:

    You’re the man! Thanks again! :)

    AG

  2. Brian says:

    Awesome Thanks a bunch!…Just ran into this issue on a site.

  3. Craig says:

    Wow. Worked like a charm. I also… just ran into this issue.

    Thanks for taking a little time to post about this.

  4. Dan Gaish says:

    Nice slice of code – saved me some trouble. Many thanks for writing this up.

  5. Glad it helped you guys, glad to know that I was right for putting it out there.

  6. Joe says:

    Thanks lot, this really helped me out!

  7. Jason says:

    Gotta hand it to you Richard, you’ve really made my day after getting very frustrated with having the blog highlighted on a 404! Top man!

  8. Spanky says:

    duuno why… but this codesnippet dosenĀ“t work for me…

  9. Geert says:

    You wrote this 3 years ago and it’s still relevant today! The “problem” persists (although it may or may not be a problem depending on the situation) and there still doesn’t seem to be a better way to handle this. Thanks a lot for posting your solution!

  10. Geert says:

    For those using the navigation menus introduced in WordPress 3.0 (and you should, most themes do), try this code:


    function modify_css_class($css_class, $page) {
    if (is_404() || is_search()) {
    foreach ($css_class as $k=>$v) {
    if ($v=='current_page_parent') unset($css_class[$k]);
    }
    }
    return $css_class;
    }
    add_filter('nav_menu_css_class','modify_css_class',10,2);

    This goes in your theme’s functions.php file.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>