I have a menu listing all of my products categories in Woocommerce. I need to add an attribute data-relation
to each <li>
tag and the value for each one would be the ID of the category.
I success to do it for the <a>
tag. Does it exist a filter for <li>
?
Here is the PHP filter function:
function category_menu_id_attribute ($atts, $item, $args) {
$atts['data-relation'] = $item->object_id;
return $atts;
}
add_filter('nav_menu_link_attributes', 'category_menu_id_attribute', 10, 3);
Thank you for the help.
nav_item
post, not the term/product category. Nav menus are a taxonomy, and each item in the menu is a post of typenav_item
. Why do you need this?Walker_Nav_Menu::start_el()
which builds the<li>
tag.object_id
is the ID of the "object" (post, category, tag, etc.) originally represented by the nav menu item, and the nav menu item ID is stored in$item->ID
and$item->db_id
. Seewp_setup_nav_menu_item()
.<a>
tag. Thanks again!