最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - How can I add a custom class to only nav sub-menu > li > a items in WordPress?

matteradmin5PV0评论

I've this DOM here:

<ul class="sub-menu">
    <li id="menu-item-1424" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat">
        <a href="#">Item</a>
    </li>
</ul>

What I need to do is, to add a custom class (has-ripple) to only the sub-menu > a items:

<ul class="sub-menu">
    <li id="menu-item-1424" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat">
        <a href="#" class="has-ripple">Item</a>
    </li>
</ul>

This is what I've tried:

add_filter( 'nav_menu_link_attributes', 'nav_menu_link_class', 10, 3 );
function nav_menu_link_class( $atts, $item, $args ) {
    $class         = 'has-ripple';
    $atts['class'] = $class;

    return $atts;
}

This works but it also add the class to non-submenu a items outside of the dropdown. So how can I do this right?

I've this DOM here:

<ul class="sub-menu">
    <li id="menu-item-1424" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat">
        <a href="#">Item</a>
    </li>
</ul>

What I need to do is, to add a custom class (has-ripple) to only the sub-menu > a items:

<ul class="sub-menu">
    <li id="menu-item-1424" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat">
        <a href="#" class="has-ripple">Item</a>
    </li>
</ul>

This is what I've tried:

add_filter( 'nav_menu_link_attributes', 'nav_menu_link_class', 10, 3 );
function nav_menu_link_class( $atts, $item, $args ) {
    $class         = 'has-ripple';
    $atts['class'] = $class;

    return $atts;
}

This works but it also add the class to non-submenu a items outside of the dropdown. So how can I do this right?

Share Improve this question asked Mar 26, 2019 at 15:03 Johnny97Johnny97 2147 silver badges18 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

To be applied to sub-menu items only, your code should be:

add_filter( 'nav_menu_link_attributes', 'nav_menu_link_class', 10, 2 );
function nav_menu_link_class( $atts, $item ) {
    if( !$item->has_children && $item->menu_item_parent > 0 ) {
        $class         = 'has-ripple';
        $atts['class'] = $class;
    }

    return $atts;
}

Try this : Documentation

add_filter( 'nav_menu_link_attributes', 'nav_menu_link_class', 10, 4 );

function nav_menu_link_class(  $atts, $item, $args, $depth ) {

      if ( 0 !== $depth ) {
         $class         = 'has-ripple';
         $atts['class'] = $class;
      }
    return $atts;
}

I hope this may help.

Post a comment

comment list (0)

  1. No comments so far