0

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

enter image description here

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')

0

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;
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.