Adding class “current_page_item” for custom post type menu

I am using the following to help display a list of posts created in a custom post type.

$args = array(
  'post_type'=>'portfolio',
  'title_li'=> __('Portfolio')
);
wp_list_pages( $args ); 

However a class is not being added to the list item of the current page (current_page_item). Any ideas of how I could make this happen?

Related posts

Leave a Reply

6 comments

  1. You can use this code to make the parent post type active in the menu:

    <?php
    
    add_action('nav_menu_css_class', 'add_current_nav_class', 10, 2 );
    
    function add_current_nav_class($classes, $item) {
    
        // Getting the current post details
        global $post;
    
        // Getting the post type of the current post
        $current_post_type = get_post_type_object(get_post_type($post->ID));
        $current_post_type_slug = $current_post_type->rewrite[slug];
    
        // Getting the URL of the menu item
        $menu_slug = strtolower(trim($item->url));
    
        // If the menu item URL contains the current post types slug add the current-menu-item class
        if (strpos($menu_slug,$current_post_type_slug) !== false) {
    
           $classes[] = 'current-menu-item';
    
        }
    
        // Return the corrected set of classes to be added to the menu item
        return $classes;
    
    }
    
    ?>
    

    I found this code in this Gist and it worked for me – it marks the parent navigation as active when on a custom post type single post.

  2. You need to add this to your functions.php:

    function kct_page_css_class( $css_class, $page, $depth, $args, $current_page ) {
      if ( !isset($args['post_type']) || !is_singular($args['post_type']) )
        return $css_class;
    
      global $post;
      $current_page  = $post->ID;
      $_current_page = $post;
      _get_post_ancestors($_current_page);
    
      if ( isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors) )
        $css_class[] = 'current_page_ancestor';
      if ( $page->ID == $current_page )
        $css_class[] = 'current_page_item';
      elseif ( $_current_page && $page->ID == $_current_page->post_parent )
        $css_class[] = 'current_page_parent';
    
      return $css_class;
    }
    add_filter( 'page_css_class', 'kct_page_css_class', 10, 5 );
    

    Via http://kucrut.org/wp_list_pages-for-custom-post-types/

  3. I’d like to add a workaround that worked in my situation.

    I had Custom Post Type with Custom Taxonomy and wanted to list these posts as long as they were in a particular Custom Category – with current page class on the li.

    The code above* produced a list of all posts, but didn’t filter categories.
    * [editor note] Answers may vary its order, not sure what code is being referred.

    My solution comes from that code, not sure if it’s best practice, but it works..

    <?php
    // get current page/post ID
    $pageID = get_the_ID();
    query_posts( array( 'post_type' => 'developments', 'custom_cat' => 'current' ) );
    if ( have_posts() ) : 
        while ( have_posts() ) : 
            the_post();
            // test if current page/post ID matches
            if ( $post->ID == $pageID ) {
                $class = 'current_page_item';
            } else {
                $class = '';
            }
    ?>
            <li <?php if ($class != '') echo 'class="'.$class.'"'; ?>>
                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
            </li>
    <?php 
        endwhile; 
    endif; 
    wp_reset_query(); 
    ?>
    
  4. if(site_url()."/".get_post_type() == $menu_item->url || site_url()."/".$post->post_name == $menu_item->url){#STUFF TO IDENTIFY ON FRONT}
    

    helps me adding currently active class to current menu item when I LOOP wp_get_nav_menu_items as foreach($primary_nav_menu_items as $n => $menu_item){#do something} . It may help someone.