wp_nav_menu returns menu list in ascending order. How can I arrange the menu same as dashboard menu

 ?php
              wp_nav_menu(array(
                  'theme_location' = 'primary',
                  'container' = 'ul',
                  'menu_class'= 'top-menu'
                  /* 'walker' = new Walker_nav_Primary() */
                  )
              );                  
          ?

This is a simple wp_nav_menu

You can see ascending order from above example

I have done this in WP.

wp_nav_menu gives me ascending order menu. How can I arrange as dashboard?

Please help.

Topic php menus html5 Wordpress

Category Web


As far as I can tell, if the theme_location is incorrect, the menu defaults to alphabetical order, rather than the order set on your menu screen.

I can see you have a tick next to Primary Menu in the menu settings, but the actual key / slug might be different in your theme. For example:

register_nav_menus( array(
    'customkey' => __( 'Primary Menu', 'twentysixteen' ),
    'social'  => __( 'Social Links Menu', 'twentysixteen' ),
) );

If you do a search for:

register_nav_menus

in the code of your theme, you should be able to find the correct value.

You can then add this to your code, for example:

wp_nav_menu(array(
              'theme_location' => 'customkey',

Please use below code in your theme funcation.php file

function my_asc_nav_menu($menu, $args) {
if (isset($args->reverse) && $args->reverse) {
return array_reverse($menu);
}
return $menu;
}
add_filter('wp_nav_menu_objects', 'my_asc_nav_menu', 10, 2);

And also use your wp_nav_menu function as below:

 wp_nav_menu(array(
              'theme_location' => 'primary',
              'container' => 'ul',
              'menu_class'=> 'top-menu',
              'reverse' => FALSE,

              )
          );       

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.