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 have a custom post type "product". I need to have its index accessible from the main menu, so i 've added a Custom Menu entry, simply stating "/works/" as url value. Problem is: if i'm looking at a single product, the main index menu link does not receive the current_menu_ancestor class. Any idea how to fix that?

register_post_type( 'product',      
                array(
            'labels' => array(
                'name' => __( 'Works' ),
                'singular_name' => __( 'Work' ),
                'add_new' => _x('Add New', 'Work'),
                'add_new_item' => __('Add New Work'),
                'edit_item' => __('Edit Work'),
                'new_item' => __('New Work'),
                'view_item' => __('View Work'),
                'search_items' => __('Search Works'),
                'not_found' =>  __('No Work found'),
                'not_found_in_trash' => __('No Work found in Trash'), 
                'menu_name' => 'Works'
            ),
            'public' => true,
            'publicly_queryable' => true,
            'exclude_from_search'=>false,
            'show_ui '=>true,
            'query_var' => true,
            'show_in_menu '=>true,
            'show_in_nav_menus '=>true,
            'has_archive' => 'works',
            'rewrite' => array('slug'=>'work','with_front'=>false),
            'menu_position' => 2,
            'menu_icon' => get_stylesheet_directory_uri() . '/library/images/custom-post-icon.png',
            'capability_type' => 'post',
            'hierarchical' => false,
            'supports'=>array('title','editor','thumbnail','excerpt'),
            'taxonomies'=>array('post_tag')
        )
    ); 
share|improve this question

1 Answer 1

up vote 2 down vote accepted

Put this in your functions.php:

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 );

This code check if you are on a page product archive or an single product and if so it adds the class current-menu-item

share|improve this answer
    
Right now it doesn't seem to work, but i'll play with it. I'm not sure i need to check against $menu_item->post_name. Thank you. –  pixeline Jun 23 '12 at 23:12
    
you got it right, i had a typo. Thanks a lot! –  pixeline Jun 23 '12 at 23:27

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.