I've made a menu in WordPress backend with 3 levels depth. (four main tabs with two sub level items)

I would like to know how I can get the number of tabs created automatically and add this number to a class on the ul (i.e: .item-4)

Any ideas? I'm already using a custom walker.

1 Answer 1


You can use the wp_get_nav_menu_object to retrieve the menu as an object, which you can subsequently establish the length of. Like this:

$wpse140949_menu = wp_get_nav_menu_object( 'your-menu-name-or-slug' );
$tabcount = $wpse140949_menu->count;

You can the use this on your call to wp_nav_menu:

wp_nav_menu ( array (
  'menu_class' => 'item-' . $tabcount,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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