Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

Adding Conditional Classes to Menu Items


/**
 * Filter the CSS class for a nav menu based on a condition.
 *
 * @param array  $classes The CSS classes that are applied to the menu item's <li> element.
 * @param object $item    The current menu item.
 * @return array (maybe) modified nav menu class.
 */
function wpdocs_special_nav_class( $classes, $item ) {
    if ( is_single() && 'Blog' == $item->title ) {
        // Notice you can change the conditional from is_single() and $item->title
        $classes[] = "special-class";
    }
    return $classes;
}
add_filter( 'nav_menu_css_class' , 'wpdocs_special_nav_class' , 10, 2 );
Source by developer.wordpress.org #
 
PREVIOUS NEXT
Tagged: #Adding #Conditional #Classes #Menu #Items
ADD COMMENT
Topic
Name
2+4 =