Forums
Forums / Plugins / count wp_nav_menu items
(@ahoereth)
15 years, 11 months ago
Hey there, I want to fit the menu differently to the layout depending on how many first level items it got. So i just tried to count them, but my script always returns 0.
$wp_nav_menu_to_count = wp_nav_menu( array( 'echo' => 'false', 'depth' => '1' ) ); $wp_nav_menu_counter = substr_count($wp_nav_menu_to_count, 'menu-item'); echo $wp_nav_menu_counter;
It worked likewise with wordpress 2.9, but i used a get function there.
Could someone please help me? Still searching for an solution..
15 years, 10 months ago
Still searching for a solution.
Someone?
(@pephers)
$menu_to_count = wp_nav_menu(array( 'echo' => false, 'theme_location' => 'menu' )); $menu_items = substr_count($menu_to_count,'class="menu-item '); echo $menu_items;
This should work. Change the theme_location to what your menu is called.
Greetings, Chiel Robben
The topic ‘count wp_nav_menu items’ is closed to new replies.