I have created a hierarchical custom post type called events and have used wp_list_pages to list the sub-posts for each event post (See below). This navigation works great, but I cannot figure out how to highlight the menu items based on the post being viewed. I normally use .current_menu_item, but this class is not generated for CPT.

Is there any way I can add this class to the link for the current post? Thank you for your help!

//If the post has a parent, get the parent id
if($post->post_parent) {
    $args = array(
      'title_li'  => '',
      'child_of'  => $post->post_parent,
      'post_type' => 'events',
      'echo'      => 0
    //Create a variable for the parent post id
     $top_page = $post->post_parent;
//If the post is a parent, get the post id
else {
    $args = array(
      'title_li'  => '',
      'child_of'  => $post->ID,
      'post_type' => 'events',
      'echo'      => 0
    //Create a variable for the post id
    $top_page = $post->ID;  
//List child pages
$post_children = wp_list_pages( $args );
//Get the title of the the parent page
$top_title = get_the_title($top_page); 
//Get the of permalink the parent page
$top_link = get_permalink($top_page);

    //If there are children, list them in a side nav
    if ( $post_children ) {
    <!--Sub-page side nav-->
    <div id="inline-side-nav">
        <a href="<?php echo $top_link; ?>"><h3><?php echo $top_title; ?></h3></a>
            <?php echo $post_children ?>
  • I know for pages specifically the class that's assigned is current_page_item, not current_menu_item, I assume you've verified that no extra classes are assigned for a CPT?
    – Milo
    Jan 2, 2012 at 17:47
  • Each <li> is given a unique class (i.e. class="page_item page-item-1192"), but no specific class is assigned based on the current post.
    – Susan
    Jan 2, 2012 at 18:30
  • 1
    You'll need to create a custom walker then I suppose.
    – Milo
    Jan 2, 2012 at 19:04
  • I have not worked with the Walker Class class before. I see from codex.wordpress.org/Function_Reference/Walker_Class that it is used to establish a tree structure hierarchy, but I am unsure how to implement it in this case. Do you have any suggested references? Thanks for your help!
    – Susan
    Jan 2, 2012 at 19:58
  • 1
    I suppose you are better off using get_posts(), and iterate on the result manually rather than using wp_list_pages.
    – ifdion
    Aug 17, 2012 at 11:52


Your Answer

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