Your code doesn’t because the global $post
refers to the post/page being viewed (if there is one…). What is passed with nav_menu_css_class
is a menu item – this is a particular post type. Among its properties are:
object
which stores:
- the post type if the link refers to a post
- the taxonomy if the link refers to a taxonomy-term page
- ‘custom’ if its a custom link
It also stores the object_id
- the post id if the link refers to a post
- the term id if the link refers to a taxonomy-term page
- the post id of the custom link
So in what you want to do is check if the term actually refers to a post (or a custom post type), and then pass the object_id
to the get_post_meta()
:
function wpse51076_special_nav_class($classes, $item){
if( 'post' == $item->object ){
$colorcode = get_post_meta($item->object_id, 'color_dropdown', true);
$classes[] = $colorcode;
}
return $classes;
}
add_filter('nav_menu_css_class' , 'wpse51076_special_nav_class' , 10 , 2);