Highlighting page in menu on custom post types

I have a page called “Portfolio”. I use this page to show the archive for my custom post type called “Works”. I do this by displaying the portfolio page on with a custom template called “Work archive”.

I would like to highlight the Portfolio page in my menu when I am on a single post of Works.

Read More

Can You help me?

Related posts

Leave a Reply

3 comments

  1. This could help you

    function change_page_menu_classes($menu){
        global $post;
        if (get_post_type($post) == 'portfolio')
        {
            $menu = str_replace( 'current_page_parent', '', $menu ); // remove all current_page_parent classes
            $menu = str_replace( 'page-item-366', 'page-item-366 current_page_parent', $menu ); // add the current_page_parent class to the page you want
        }
        return $menu;
    }
    add_filter( 'wp_page_menu', 'change_page_menu_classes', 0 );
    

    Source

  2. Hey I dunno if this is still relevant but I came across this and it worked great. I’m using the roots theme with a post type of “projects”

    // Remove active class from menu
    function remove_active_class($class) {
    return ( $class == 'active' ) ? FALSE : TRUE;
    }
    
    // Add active class to menu of post type single template
    function add_class_to_wp_nav_menu($classes) {
    
    if( is_singular( 'projects' ) ) {
    
        $classes = array_filter( $classes, 'remove_active_class' );
    
        if( in_array( 'menu-projects', $classes) ) {
          $classes[] = 'active';
        }
    } elseif( is_singular( 'resources' ) ) {
    
        $classes = array_filter( $classes, 'remove_active_class' );
    
        if( in_array( 'menu-resources', $classes) ) {
          $classes[] = 'active';
        }
    }
    
    return $classes;
    }
    add_filter('nav_menu_css_class', 'add_class_to_wp_nav_menu');
    
  3. add_filter( 'nav_menu_css_class', 'namespace_menu_classes', 10, 2 );
    function namespace_menu_classes( $classes , $item ){
    if ( get_post_type() == 'attorneys' ) {
        $classes = str_replace( 'current_page_parent', '', $classes );
        if ( $item->url == '/attorneys' ) {
        // Replace "attorneys" with your code
        if(preg_match('/attorneys/', $item->url)) {
            $classes = str_replace( 'menu-item', 'menu-item current_page_parent', $classes );
        }
    }
    return $classes;
    

    }

    Altered from here: https://wordpress.org/support/topic/custom-post-type-highlighting-current-menu-item