Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a WP 3.x site that I'm using mainly as a CMS. The menu links to the various WordPress pages.

One of the pages, "News", is the "posts" page.

However, when clicking on one of the posts on the News page, the "News" item in the menu loses highlighting because the post isn't treated as a child of "news".

Is there an easy way to override the currently highlighted item in the menu? Ideally by forcing a page ID upon it.

share|improve this question
    
Are you using wp_list_pages or wp_nav_menu to output the menu? –  TheDeadMedic Jan 7 '12 at 12:10
    
if its an internal interface and you dont mind hard coding it why not use CSS?? doesent that menu item have a "current" or similer class when selected? –  Sagive SEO Jan 7 '12 at 12:40
    
@Sagive I'm using WP's built-in menu. The menu doesn't have a "current" class because WP doesn't recognize posts as children of the "posts" page. That's the issue - I'm using the "current" class everywhere else and I want to be able to use it in posts as well –  Pekka 웃 Jan 7 '12 at 13:09
    
i see... then maybe (just fishing here) if your using wp menus you could point to a category and not a post page.. that way it should work.. is there a link i can watch it? is it online? –  Sagive SEO Jan 7 '12 at 13:55

1 Answer 1

up vote 2 down vote accepted

Since I assume you're using wp_nav_menu(), there should be a class current_page_parent which is applied to your posts' page menu item when viewing a single post.

You can use this class to style the 'active' state, much like you're probably doing with current_page_item at the moment.

If (for whatever reason) you must have current_page_item added too, you can filter the nav_menu_css_class:

/**
 * Add the class 'current_page_item' to 'page for posts' menu item if we're
 * viewing a single post.
 * 
 * @param array $class
 * @param object $item The current menu item.
 * @return array
 */
function add_current_class_to_posts_page( $classes, $item )
{
    static $posts_page;

    if ( ! is_single() )
        return $classes;

    if ( ! isset( $posts_page ) )
        $posts_page = get_option( 'page_for_posts' ); // cache as we may be calling this a lot!

    if ( $item->object == 'page' && $item->object_id == $posts_page )
        $classes[] = 'current_page_item'; // this is the posts page!

    return $classes;
}
add_filter( 'nav_menu_css_class', 'add_current_class_to_posts_page', 10, 2 );
share|improve this answer
    
Re your edit, ahh, that looks interesting, I think I can work with that: Posts are not being treated as children of the posts page so neither current_page_parent nor current_page_childrn turn up, but I should be able to hack it using your code sample. I will try out and get back to you. –  Pekka 웃 Jan 7 '12 at 14:42
    
WordPress adds classes in a context-aware fashion. If you're viewing a post, it will add the class current_page_parent if the item is the 'posts page' - check out your source if you don't believe me! –  TheDeadMedic Jan 7 '12 at 14:43
    
Oh maaan, shame on me! I didn't check properly, it indeed is the way you say. I don't understand how I could overlook that. Sorry and thanks! –  Pekka 웃 Jan 7 '12 at 14:48
    
Makes me wonder why $item->current or $item->current_parent isn't true. Anyway: Elegant solution, ideal muplugin. +1 –  kaiser Nov 23 '12 at 3:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.