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 have a custom post type called Books and a page structure like this:

Products & Services
- Services
- Products
-- Books

I am using Wordpress custom Menus for the 1st level navigation (displayed horizontally), and wp_list_pages to print out the subnavigation based on the page selected from the 1st level of navigation (the submenu is displayed vertically).

To make things more interesting/worse, I am only displaying Books in the submenu if I am viewing the Products or Books page.

Now, I would like to add the active and current_page_item/current_page_ancestor classes to the Products & Services, Products and Books when I am viewing a single Book (custom post type).

How should I do it?

share|improve this question

You can add a custom class via the nav_menu_css_class filter, like so:

function wpa_65178_nav_class( $classes, $item )
    if( is_singular('book') && $item->title == 'Products & Services' )
        $classes[] = 'current_page_ancestor';
    return $classes;
add_filter( 'nav_menu_css_class', 'wpa_65178_nav_class', 10, 2 );
share|improve this answer

First thing that comes to mind is to use the built-in walker the Nav Menus use. That would be something like that

$custom_walker = new Walker_Nav_Menu();
wp_list_pages( array( 'walker' => $custom_walker ) );

Hope that's helpful

share|improve this answer

You can use a php conditional to load a jQuery script for when the custom post type is active, and then use jQuery to addClass the current-menu-item class to the menu items by ID number. Look in your page source to find the menu IDs for each item. These will change if you change the menu; there' no way around that.

<?php if ( 'book' == get_post_type()) { ?>

<script type="text/javascript">

jQuery(function($) {
$(document).ready(function() {
$('#menu-main-menu li.menu-item-1000').addClass('current-menu-item');
}); });


<?php } ?>
share|improve this answer

Your Answer


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.