I need to get the nr. of items in the primary navigation menu. I need this to determine the width in percentage of each list item. So I also need to change/add to the css width attribute of the list items somehow, which I haven't figured out how to do yet either... As a first attempt I've tried the function wp_get_nav_menu_object. I am developing a child theme off of the twentyfifteen template. I have made a custom home page where I load the primary navigation (copied straight from the sidebar.php file in the original theme folder) and call wp_get_nav_menu_object straight after that using the code snippet

<?php if (has_nav_menu('primary')) : ?>   
   <nav id="site-navigation" class="main-navigation" role="navigation">
       // Primary navigation Menu
         'menu_class' => 'nav-menu',
         'theme_location' => 'primary',
       // none of these seem to work...
       //$menu = wp_get_nav_menu_object('menu-primary-menu');
       $menu = wp_get_nav_menu_object('primary');
       echo $menu->count;
   </nav> <!-- /.main-navigation --> 
<?php endif; ?>

I know I could go with a jQuery/JavaScript solution (which is what I'm doing at the moment in fact), but that's not ideal. I'm beginning to suspect that for what I want to do I need to create my own custom primary navigation menu with the help of the Walker class. Could anybody advise me on what the best approach would be?

  • nr.? sorry for asking. I am not good with abbreviation..
    – Sisir
    Jul 2, 2015 at 10:18
  • nr = number ( I guess :-) ) Jul 2, 2015 at 10:40

1 Answer 1


You are not providing the correct parameter to wp_get_nav_menu_object() function. It needs the menu ID or menu slug but you are providing the menu theme location. Obviously, I can not tell you what is the ID or slug of your menu.

One way of getting the menu ID of the menu assigned to theme location is using get_nav_menu_locations():

$menu = wp_get_nav_menu_object( get_nav_menu_locations()['primary'] );
echo $menu->count;

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.