is_single OR is_category trouble
-
Hi,
To highlight a menu-item (BLOG) when a single post, a category or the archive is viewed, I have this:
add_filter( 'nav_menu_css_class', 'add_custom_class', 10, 4 ); function add_custom_class( $classes = array(), $menu_item = false ) { if ( is_single() && 44 == $menu_item->ID && ! in_array( 'current-menu-item', $classes ) ) { $classes[] = 'rasmus-test'; } return $classes;It does add the class rasmus-test to that specifik menuitem when a single post is viewed (it has ID 44).
If i change it to this:
add_filter( 'nav_menu_css_class', 'add_custom_class', 10, 4 ); function add_custom_class( $classes = array(), $menu_item = false ) { if ( is_category() && 44 == $menu_item->ID && ! in_array( 'current-menu-item', $classes ) ) { $classes[] = 'rasmus-test'; } return $classes;It works just fine as well for viewing a category.
But – if I do this:
add_filter( 'nav_menu_css_class', 'add_custom_class', 10, 4 ); function add_custom_class( $classes = array(), $menu_item = false ) { if ( is_single() || is_category() && 44 == $menu_item->ID && ! in_array( 'current-menu-item', $classes ) ) { $classes[] = 'rasmus-test'; } return $classes;… the class rasmus-test is added to every menu-item.
What is wrong?
A bonus-question:
Im not happy about this:
&& 44 == $menu_item->IDI like it to be dynamick; Like based on the pagetemplate. How do I do that?
thx
Rasmus
Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
The topic ‘is_single OR is_category trouble’ is closed to new replies.