I'm outputting a list of pages in my custom post type — a kind of tree — using wp_list_pages().

All's going well, except now I want to put some classes in the li's for styling. Even that's not going too bad with the help of this guy:

function tsep_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;

  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', 'tsep_page_css_class', 10, 5 );

This basically prints out current-page-item, ancestor or parent in my <li>. This part works really well. What I want however, is to print out taxonomy terms from my "content-type" taxonomy in the <li> classes too. So, one might look like this: `

I've got a start on it:

$terms = get_the_terms( $post->ID, 'content-type' );

        if ( $terms && ! is_wp_error( $terms ) ) : 

            $content_type = array();

            foreach ( $terms as $term ) {
                $content_type[] = $term->name;

            $the_content_type = join( ", ", $the_content_type );


But in the end, $the_content_type doesn't seem to return anything.

How can I get $the_content_type to return a value, and how would I put that value in the $css_class[] above?

Hope some of this made sense!


1 Answer 1


Doesn't WordPress already at the class current_page_parent, current_page_ancestor and current_page_item (see source). The filter you are using is called on 1046.

As for your question, you create the array $content_type and then attempt to make a string out of $the_content_type.


 function wpse56088_page_css_class( $css_class, $page, $depth, $args, $current_page ){
     $terms = get_the_terms( $page->ID, 'content-type' );
     if ( $terms && ! is_wp_error( $terms ) ){
        $term_names = wp_list_pluck($terms,'name');

        //Add term names to array of classes
        $css_class = array_merge($css_class, $term_names);
     return $css_class;
add_filter( 'page_css_class', 'wpse56088_page_css_class', 10, 5 );

Note $css_class is suppose to be an array of class names.

  • I'll try that now. I think that filter on 1046 only works for actual pages, not custom pages. I think
    – saltcod
    Jun 21, 2012 at 19:14
  • Close. It adds the class fine, but does so based on the $post->ID, instead of itself. IE: It adds the tax term associated the current post to each <li> in the output.
    – saltcod
    Jun 21, 2012 at 19:17
  • Not all of these are Sections: cl.ly/2a0l3V1G3A3f3M1H3H0b. Nor are they all Topics: cl.ly/3F0Z3c2H2y1S3P3h0l47
    – saltcod
    Jun 21, 2012 at 19:18
  • My bad - get_the_terms should reference the passed $page variable, not the (undefined) $post variable. The latter is returning null and so in the absence of an ID, WordPress uses the current page. Editing answer... Jun 22, 2012 at 9:54
  • wp_list_pluck!!!!! Of course. Never thought of doing it this way. Huge thanks!
    – saltcod
    Jun 22, 2012 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.