Advertisement
salmancreation

wp_nav_menu()

Oct 10th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.20 KB | None | 0 0
  1. <?php $defaults = array(
  2.     'theme_location'  => ,
  3.     'menu'            => ,
  4.     'container'       => 'div',
  5.     'container_class' => 'menu-{menu slug}-container',
  6.     'container_id'    => ,
  7.     'menu_class'      => 'menu',
  8.     'menu_id'         => ,
  9.     'echo'            => true,
  10.     'fallback_cb'     => 'wp_page_menu',
  11.     'before'          => ,
  12.     'after'           => ,
  13.     'link_before'     => ,
  14.     'link_after'      => ,
  15.     'items_wrap'      => '<ul id=\"%1$s\" class=\"%2$s\">%3$s</ul>',
  16.     'depth'           => 0,
  17.     'walker'          =>
  18. );
  19. ?>
  20.  
  21. <?php wp_nav_menu( $defaults ); ?>
  22.  
  23.  
  24.  
  25. ======================
  26.  
  27.  
  28.  
  29. <?php
  30.                             wp_nav_menu( array(
  31.                                 'theme_location' => 'menu-1',
  32.                                 'menu_id'        => 'primary-menu',
  33.                                 'menu_class'     => 'nav navbar-nav lgx-nav navbar-right',
  34.                                 'container'       => 'ul'
  35.                             ) );
  36.                             ?>
  37.  
  38. =====================
  39.  
  40. has_nav_menu()
  41. This function is great for only displaying a particular menu if that menu has been assigned to your theme location. For instance, you may want to create a top navigation on your theme for lesser navigation items that a user may not want in their main navigation. This could be things like a home link, "Advertise With Us", or other lower calls to action. But as a theme distributor, if you don't know if that's going to be something the user wants to use, simply use a condition like so:
  42.  
  43. 1
  44. 2
  45. 3
  46. if (has_nav_menu('top-menu')) {
  47.     wp_nav_menu('theme_location='top-menu');
  48. }
  49. wp_get_nav_menu_items()
  50. This function will return an array of items from a particular menu. This may be particular useful if you want to build a custom menu list without using a Walker Class. You lose a lot of functionality such as the menu item's current class, but it's a great way to loop through an array of menu items for a simple solution.
  51.  
  52.  
  53. -------------------------------------
  54.  
  55.  
  56. Once we have our descriptions filled out, we'll need to create the walker class and add it to the wp_nav_menu() parameters. We'll call the class description_navigation so our complete parameters code should look like this:
  57.  
  58. $params = array(
  59.     'theme_location' => 'primary',
  60.     'menu_id' => 'nav',
  61.     'walker' => new description_walker()
  62. );
  63. wp_nav_menu($params);
  64.  
  65. ===============================
  66.  
  67. Container Class and Container ID
  68. As you can pretty much guess, these parameters are used to set a class and an ID to the container. Since we're omitting this altogether, we have no need to define values.
  69.  
  70. Menu Class and Menu ID
  71. These work just like the previous parameters except this time we definitely want to set an ID of "nav" because that is the ID we'll use in our stylesheet to style the navigation bar.
  72.  
  73. 1
  74. 2
  75. 3
  76. 4
  77. 5
  78. $params = array(
  79.     'theme_location' => 'primary',
  80.     'container' => false,
  81.     'menu_id' => 'nav'
  82. );
  83. Echo
  84. You can use this parameter to tell whether you want to display (echo) the results, or return it for use in PHP. This item is boolean so to return it simply set this parameter to 0.
  85.  
  86. Fallback CB
  87. This is a callback function that you can fallback to if no menu is found. By default it uses the old stand by wp_page_menu() and passes all of the same parameters to this function as well.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement