Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I add a title before the ul using wp_nav_menu?

Tags:

wordpress

menu

I need to add a title to my menu, which is being built by wp_nav_menu...something like this:

<div class="container">
  <div class="title">My Menu Title</div>
  <ul class="menu">
    <li class="item"><a href="#">Item 1</a></li>
    <li class="item"><a href="#">Item 2</a></li>
    <li class="item"><a href="#">Item 3</a></li>
  </ul>
</div>

I find it strange that this isn't included by default :s

like image 359
SomethingOn Avatar asked Oct 26 '11 01:10

SomethingOn


People also ask

How do I get the menu title in WordPress?

Click on any of your menu items. You will see a text box for Title Attribute. Fill out the text box and repeat these steps for every navigation menu item. Once you have updated all of your navigation items click on the “Save Menu” button.

How do I add a class to a tag in wp nav menu?

Optionally, you may want to add the option to add classes to list items: function add_menu_list_item_class($classes, $item, $args) { if (property_exists($args, 'list_item_class')) { $classes[] = $args->list_item_class; } return $classes; } add_filter('nav_menu_css_class', 'add_menu_list_item_class', 1, 3);


2 Answers

This should work for you!

wp_nav_menu(
  array(
    'items_wrap' => '<div class="title">Your menu title</div><ul class="%2$s">%3$s</ul>'
  )
);
like image 135
Anonymous Avatar answered Sep 25 '22 19:09

Anonymous


This seems like a slightly broken option, I followed the guide on the wordpress codex, and a linked recommended guide, and every time, items_wrap did nothing at all, my original menu code was

<?php wp_nav_menu( array( 'theme_location' => 'primary','items_wrap' => '<ul id="%1$s" class="sf-menu %2$s">%3$s</ul>' ) ); ?>

Which didn't work and after much hair pulling, I changed to this

<?php wp_nav_menu( array( 'items_wrap' => '<ul class="sf-menu %2$s">%3$s</ul>' ) ); ?>

Which does, both are basically identical, so if it doesn't work first time don't give up, strip back and keep trying!

like image 43
Stephen Phillips Avatar answered Sep 24 '22 19:09

Stephen Phillips