Blog tab gets highlighted in nav menu for custom post types (9 posts)

  1. Serge Liatko
    Posted 3 years ago #

    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 :


    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.


  2. LucP
    Posted 3 years ago #

    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 ) ) {
    	return $classes;
  3. Serge Liatko
    Posted 3 years ago #

    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!

  4. Serge Liatko
    Posted 3 years ago #

    BTW there is no filter in there? well done!

  5. LucP
    Posted 3 years ago #

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

    But you're right, that is outdated... maybe you can create a ticket about it?

  6. Serge Liatko
    Posted 3 years ago #

    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) {
        return $classes;
  7. Serge Liatko
    Posted 3 years ago #

    yes a ticket on that would be nice

  8. LucP
    Posted 3 years ago #

    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! ;-))

  9. Serge Liatko
    Posted 3 years ago #

    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)



Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.