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 in my wordpress theme. I want to add a custom class to the nav menu for the pages created in that custom post type. I read that you can use a filter hook: "nav_menu_css_class", but my php chops are pretty limited. How do I set up that filter hook to apply only to my custom post type pages in the nav menu and give them a custom class?

share|improve this question

1 Answer 1

here is a simply example:

add_filter('nav_menu_css_class', 'auto_custom_type_class', 10, 2 );
function auto_custom_type_class($classes, $item) {

    if ($item->type_label == "CUSTOM_TYPE_NAME"){
        $classes[] = "New_Class";
    }

    return $classes;
}

just change CUSTOM_TYPE_NAME to the name of your custom post type and New_Class with the name of your class and paste this snippet in your theme's functions.php file.

share|improve this answer
    
That works. Appreciate it! –  Colin Jun 15 '11 at 21:09
    
Glad i could help. –  Bainternet Jun 15 '11 at 21:19
    
Another question: $item->type_label Where does this come from and are there other things you can apply to this variable "$item" ? –  Colin Jun 15 '11 at 21:21
1  
the $item object is passed by the apply_filters call and it holds all of the item variables. you can get a list of the by adding somewhere in the function var_dump($item); –  Bainternet Jun 15 '11 at 21:28
    
Wow, sweet! (just ran it) Good to know. Thanks again. –  Colin Jun 15 '11 at 22:06

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.