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 need the Wordpress menu to include the 'current-menu-ancestor' class to reflect that site is currently in the recipe section. Supposing I have a recipe custom post type. I have the following code in my functions.php but it's not working:

function add_active_item_classes($classes = array(), $menu_item = false){

if ( get_post_type() == 'recipe' && $menu_item->title == 'Recipes') {
$classes[] = 'current-menu-ancestor';

return $menuclasses;

Also I don't know what filter hooks I will use to have this effect? Thanks for your suggestion and assistance.

share|improve this question
up vote 0 down vote accepted

This is the final working code:

function additional_active_item_classes($classes = array(), $menu_item = false){
global $wp_query;

if(in_array('current-menu-item', $menu_item->classes)){
    $classes[] = 'current-menu-item';

if ( $menu_item->post_name == 'product' && is_post_type_archive('product') ) {
    $classes[] = 'current-menu-item';

if ( $menu_item->post_name == 'product' && is_singular('product') ) {
    $classes[] = 'current-menu-item';

return $classes;
add_filter( 'nav_menu_css_class', 'additional_active_item_classes', 10, 2 );

share|improve this answer
WTF is this? It doesn't add the current-menu-ancestor class and you declare global $wp_query but never use it. Please figure out what the hell you are doing before trying to help others. – developerbmw Jan 11 at 20:20

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.