• Hi WP Team, i’m currently working on a new site with wp and have registered several custom post types for it (they have post type archives enabled)

    the issue :

    Blog tab (a page set as Home page for blog posts in reading settings) get highligted ( get css class curent_page_parent ) for :

    is_post_type_archive(‘product’)
    is_singular(‘product’)

    NOTE there are no sub items for blog tab …

    Questions :

    1 for what reason ? I mean, why “blog” is considered as “parent” for totally independant content type?

    2 since what version ?

    3 how to fix it? Means , how i can preserve blog tab classes if the current page is related to “post” post type and remove the “current_page_parent” class if the visitor is viewing another content type?

    SUGGESTION : guys , do not try to hide the thing with css, it will not fix the background of the problem.

    thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • Yeah, that’s a bit annoying; you really don’t have that much control over a nav-menu… except with filters.

    Try something like this in your functions.php:

    add_filter('nav_menu_css_class', 'mytheme_css_class', 10, 2);
    
    function mytheme_css_class($classes = array(), $menu_item = false ){
    
    	if ( is_single('product') && $menu_item->ID == YOUR_BLOG_MENU_ID && in_array( 'current-menu-item', $classes ) ) {
    	    unset($classes['current-menu-item']);
    	}
    	return $classes;
    }
    Thread Starter Serge Liatko

    (@sergeliatko)

    well, now imagine i have plugs for clients that create their content types? hwo would i code this stuff?

    the simplier way (not the best one as implies corehacking) is to comment out the backward compatibility for adding curent_parent_item class to home page for blog posts if visitor is on anything but the static page (post type === page) :

    see the nav-menu-template.php from line 398 :

    // back-compat with wp_page_menu: add "current_page_parent" to static home page link for any non-page query
    		if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id )
    			$classes[] = 'current_page_parent';

    even the comment is little “outdated” as “static home page” here is actually dynamic home page for posts… those who care for support menu highlights in themes working with wp_page_menu() …. update the theme !

    to fix :

    commented out the snippet and it has dissapeared!

    NOTE : would be nice to deprecate it once for all!

    Thread Starter Serge Liatko

    (@sergeliatko)

    BTW there is no filter in there? well done!

    Huh? I don’t really understand your last comment.

    But you’re right, that is outdated… maybe you can create a ticket about it?
    http://codex.wordpress.org/Reporting_Bugs

    Thread Starter Serge Liatko

    (@sergeliatko)

    thanks for trying to help on that, you have shown me the only available filter in there (yes, there is no other way to hook into the css class creation)

    finally solved with this php (forces wp to remove the current_page_parent class if the content is not related to blog posts)

    function custom_fix_blog_tab_on_cpt($classes,$item,$args) {
        if(!is_singular('post') && !is_category() && !is_tag()) {
            $blog_page_id = intval(get_option('page_for_posts'));
            if($blog_page_id != 0) {
                if($item->object_id == $blog_page_id) {
    				unset($classes[array_search('current_page_parent',$classes)]);
    			}
            }
        }
        return $classes;
    }
    add_filter('nav_menu_css_class','custom_fix_blog_tab_on_cpt',10,3);
    Thread Starter Serge Liatko

    (@sergeliatko)

    yes a ticket on that would be nice

    Thanks for posting your new script!
    I’m going to steal that, if you don’t mind? (had this problem myself too! Never had to deliver such a flexible system as yours though -with the client being able to create posttypes and menu items-, but your script is a lot more robust! ;-))

    Thread Starter Serge Liatko

    (@sergeliatko)

    you welcome, it is all about the coding phylosophy :

    do not base you code on assumptions (see the core file backward compat example)
    try to be flexible (do not get into where you do not need to be)

    😉

    cheers,
    serge

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Blog tab gets highlighted in nav menu for custom post types’ is closed to new replies.