WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

Check this ticket: http://core.trac.wordpress.org/ticket/17590

Quickfix by husobj:

function my_page_css_class( $css_class, $page ) {
    global $post;
    if ( $post->ID == $page->ID ) {
        $css_class[] = 'current_page_item';
    }
    return $css_class;
}
add_filter( 'page_css_class', 'my_page_css_class', 10, 2 );

The quickfix might be an easier alternative? Cheers

share|improve this answer
up vote 1 down vote accepted

Found this and it works perfectly!

Dynamic navigation for custom post type (pages)

share|improve this answer

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/

share|improve this answer

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(); 
?>
share|improve this answer

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.

share|improve this answer

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.