admin管理员组

文章数量:1122846

While on any given link that has a CSS Classes value, I'd like to do something like this:

$css_classes = get_current_menu_item_css_classes();    
if ( $css_classes == 'mycustomclass' ) : // do something

Thanks

While on any given link that has a CSS Classes value, I'd like to do something like this:

$css_classes = get_current_menu_item_css_classes();    
if ( $css_classes == 'mycustomclass' ) : // do something

Thanks

Share Improve this question asked May 6, 2014 at 19:49 Tai SemTai Sem 811 silver badge11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Do not know if it's exactly what you're looking for but this short snippet adds custom classes to nav menu item :

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

         $classes[] = "my-class";

     }
     return $classes;
}

This could be modified with some conditional tags too.

EDIT: $item is often useful for example if ( $item->title == 'Contact')

You can do something this like:

// where "Top" is menu name, slug or id. But not menu location.
$menu_items = wp_get_nav_menu_items( 'Top' );

foreach ( $menu_items as $menu_item ) {
  $menu_classes = $menu_item->classes;

  foreach ( $menu_classes as $menu_class ) {
    if ( 'mycustomclass' === $menu_class ) {
        return true;
    }
  }
}

本文标签: How to get current menu item quotCSS Classesquot value