I am using wp_nav_menu($args)
and I want to add my_own_class
CSS classname to the <li>
element.
I’d like to get the following result:
<li class='my_own_class'><a href=''>Link</a>
How to do that?
I am using wp_nav_menu($args)
and I want to add my_own_class
CSS classname to the <li>
element.
I’d like to get the following result:
<li class='my_own_class'><a href=''>Link</a>
How to do that?
You must be logged in to post a comment.
No need to create custom walker. Just use additional argument and set filter for nav_menu_css_class.
For example:
Notice the new ‘add_li_class’ argument.
And set the filter on functions.php
You can add a filter for the
nav_menu_css_class
action in your functions.php file.Example:
Docs: https://developer.wordpress.org/reference/hooks/nav_menu_css_class/
HERE WordPress add custom class in wp_nav_menu links
OR
you can add class
<li class='my_own_class'><a href=''>Link</a></li>
from admin panel:Go to
YOURSITEURL/wp-admin/nav-menus.php
open
SCREEN OPTIONS
CSS CLASSES
, then you will seeCSS Classes (optional)
field in each menu link.
Adding Class to
<li>
tag without editingfunctions.php
file:Menu Items
Windowuse this filter
nav_menu_css_class
as shown belowUPDATE
To use this filter with specific menu
How about just using
str_replace
function, if you just want to “Add Classes”:Tough it is a quick fix for one-level menus or the menus that you want to add Classes to all of
<li>
elements and is not recommended for more complex menusNone of these responses really seem to answer the question. Here’s something similar to what I’m utilizing on a site of mine by targeting a menu item by its title/name:
I added a class to easily implement menu arguments. So you can customize and include in your function like this:
You could simply use
wp_nav_menu_items
to Filters the HTML list content for navigation menus.Display nav menu
Menu Registration
Apply filter
The correct one for me is the Zuan solution. Be aware to add isset to
$args->add_li_class
, however you gotNotice: Undefined property: stdClass::$add_li_class
if you haven’t set the property in all yourswp_nav_menu()
functions.This is the function that worked for me:
// Remove translation from Main Menu
This is how you easily add the new class to existing class array of you Menu wrapp
This is how I added the MainMenu of the WordPress and class to li.
Use this code in functions.php
This is a very simple way to call “li” calss and “any class” replace easily. Just follow the instructions.
Use this code in the nav area.
Then inspect your code on the browser. find wp default class and than replace “str_replace(“default_class_here”,“new_li_class_here”,$consult_menu);
| Note: $consult_menu here is my theme name, you can use any name here.
Without
walker menu
it’s not possible to directly add it. You can, however, add it by javascript.You can’t add a class directly to the LIs very easily, but is there any reason not to target them in a slightly different but equally specific way (by their parent ul)?
You can set the class or ID of the menu with the menu_class and menu_id parameters (affixed to the UL parent of the LIs) and then target the li’s via CSS that way, like so:
And for CSS:
Edit: At the time of writing in 2013 this was the easiest way to add it, but updates since then have added the feature to add CSS classes directly in the admin navigation editor. See more recent answers for details.