I want the URLs of the
nav-items
to be replaced with#
You can use the nav_menu_link_attributes
hook to set the custom href
value like so:
add_filter( 'nav_menu_link_attributes', function ( $atts, $item, $args ) {
if ( 'menu-categorias' === $args->theme_location && 'category' === $item->object ) {
$atts['href'] = '#' . get_term_field( 'slug', $item->object_id );
}
return $atts;
}, 10, 3 );
and to add the name of the category (in lowercase) to its
li
class
For that one, you can use the nav_menu_css_class
hook whereby the “name” here is the term/category slug:
add_filter( 'nav_menu_css_class', function ( $classes, $item, $args ) {
if ( 'menu-categorias' === $args->theme_location && 'category' === $item->object ) {
$classes[] = get_term_field( 'slug', $item->object_id );
}
return $classes;
}, 10, 3 );
And note that, in my examples, I checked if the theme_location
is menu-categorias
, and I also assumed your menu items are all categories (i.e. terms in the category
taxonomy).
Alternate Solution
Use a custom walker by extending the Walker_Nav_Menu
class and edit the start_el()
method which generates the li
element output (<li><a></a></li>
). See an example here.