WordPress.org

Ready to get started?Download WordPress

Forums

count wp_nav_menu items (4 posts)

  1. Alexander Hoereth
    Member
    Posted 4 years 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.

  2. Alexander Hoereth
    Member
    Posted 4 years ago #

    Could someone please help me? Still searching for an solution..

  3. Alexander Hoereth
    Member
    Posted 4 years ago #

    Still searching for a solution.

    Someone?

  4. Pephers
    Member
    Posted 3 years ago #

    $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

Topic Closed

This topic has been closed to new replies.

About this Topic