Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wp_nav_menu - add class on UL

Tags:

I am learning wordpress together with bootstrap and somehow I can't add class on UL tag.

In the screenshot, I want to add class nav nav-tabs on UL but it was added on parent div

$defaults = array(   'menu_class'=> 'nav nav-tabs',         );  wp_nav_menu( $defaults );  

Inspected element:

enter image description here

Referrence:
http://codex.wordpress.org/Function_Reference/wp_nav_menu

like image 408
JunM Avatar asked Mar 30 '14 09:03

JunM


People also ask

How do I add a class to the menu UL in WordPress?

First of all, you need to create a custom navigation menu from Appearance -> Menus . Then, use the wp_nav_menu with the following parameters: <? php $args = array( 'menu_class' => 'nav nav-tabs', 'menu' => '(your_menu_id)' ); wp_nav_menu( $args ); ?>

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);

What is the Wp_nav_menu () function used for?

Usage. wp_nav_menu( $args ); Given a theme_location parameter, the function displays the menu assigned to that location. If no such location exists or no menu is assigned to it, the parameter fallback_cb will determine what is displayed.

How do I add a custom menu code in WordPress?

To do this go to Appearance >Menus and start creating a new menu. Give the menu the title “Secondary Menu”, select “My Custom Menu” for a location and then hit the “Create Menu” button. Finally add some items to the menu (for example Menu item 1, Menu item 2, Menu item 3) and then save the menu.


2 Answers

First of all, you need to create a custom navigation menu from Appearance -> Menus.

Then, use the wp_nav_menu with the following parameters:

<?php  $args = array(     'menu_class' => 'nav nav-tabs',             'menu' => '(your_menu_id)' ); wp_nav_menu( $args );  ?> 

There's a lot you can read about Wordpress Menus. I suggest the following:
http://codex.wordpress.org/Navigation_Menus
http://www.paulund.co.uk/how-to-register-menus-in-wordpress

like image 80
andreivictor Avatar answered Oct 02 '22 07:10

andreivictor


You need to specify the container element, in our case 'ul' tag, and than specify the class that we will assign in 'menu_class'. Here is the sample code:

wp_nav_menu( array(     'theme_location' => 'top-menu',     'container' => 'ul',     'menu_class'=> '[add-your-class-here]'  ) ); 
like image 33
Brane Avatar answered Oct 02 '22 07:10

Brane