Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

<?php 
$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?

Thanks!

share|improve this question
add comment

4 Answers

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
add comment

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
add comment

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
add comment
up vote 1 down vote accepted

Found this and it works perfectly!

Dynamic navigation for custom post type (pages)

share|improve this answer
add comment

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.