I am trying to out a wp menu without ul and li and have a class added to the element.
I have tried adding this in my function.php
function add_menuclass($ulclass) {
return preg_replace('/<a /', '<a class="list-group-item"', $ulclass, 1);
}
add_filter('wp_nav_menu','add_menuclass');
And in my template I have:
<?php
$menuParameters = array(
'menu' => 'Videos',
'container' => false,
'echo' => false,
'items_wrap' => '%3$s',
'depth' => 0,
);
echo strip_tags(wp_nav_menu( $menuParameters ), '<a>' );
?>
But the output only applies the class to the first item and not all of the <a>
s as expected.
<div class="list-group">
<a class="list-group-item" href="#">First item</a>
<a href="#">Second item</a>
</div>
I am trying to achieve this, basically to apply that class to ALL my item (not sure why it applies it to only one) - No jQuery please.
<div class="list-group">
<a class="list-group-item" href="#">First item</a>
<a class="list-group-item" href="#">Second item</a>
</div>
Thanks to Sergiu Paraschiv comment the issue was in regards of limiting to 1.
Therefore it should be in function.php:
UPDATE
There is a better way actually which gives us much more control and the piece of code is provided by Jeff Starr on this post
Create your menu on wp, then remember to click the location in the menu editor then in your function you'd do:
Finally we can call our menu:
The code above is taken from the post linked above, I thought to include this answer as it appears this question has many visits.
UPDATE 2
Another solution would be (maybe the best):
header.php:
function.php:
Taking a hint from this answer which I found was the most concise about adding classes to the list items of the menus, I used nav_menu_link_attributes filter which does work well for adding classes.
In your functions.php, add:
Optionally, you may want to add the option to add classes to list items:
Now, in your template, to build a menu you just add two new arguments, e.g.:
Works well with themes with multiple menus which have different appearance.
My solution is simple, use our friend jquery
in the menu inject a custom menu_id
then use jquery to inject the missing class.
tadammmm :)
enjoy
I want add 'item' class to li should write this code: