implenton on "Adding a custom class to a menu's list item"

ساخت وبلاگ

jonathas.duarte
Member
Posted 56 minutes ago #

Hi guys.

How do i add a custom class to a menu list item?

I know I can do do this with a custom walker (even though I don't know how to use it), but isn't there a native solution?

Basically, wordpress adds the class menu-item to list items, but I need the class navTreeItem instead.

I know I could add the class manually on the admin's interface, but I don't want my users doing that. So, any solution?

implenton
Member
Posted 36 minutes ago #

Hello jonathas.duarte,

In case you want to add the navTreeItem class to all li's you can use the page_css_class filter. https://developer.wordpress.org/reference/hooks/page_css_class/

An example implementation would be this:

function menu_css_class($css_classes){ $css_classes[] = 'navTreeItem'; retu $css_classes;
}
add_filter("page_css_class", "menu_css_class", 10);

You can take a look at thi post a well: https://wpquicktips.wordpress.com/2010/02/04/custom-css-class-for-your-menu-items/

I hope this helps.

jonathas.duarte
Member
Posted 10 minutes ago #

Thanks! Now I understand how to use hooks :)

However, what worked for me was the 'nav_menu_css_class' filter, since I wanted this behavior on all list items on the menus (not only on page items).

implenton
Member
Posted 1 minute ago #

Hello jonathas.duarte,

I am glad you sorted out.

Cheers.

WordPress ...
ما را در سایت WordPress دنبال می کنید

برچسب : نویسنده : استخدام کار wpss بازدید : 180 تاريخ : دوشنبه 24 خرداد 1395 ساعت: 5:20