最新消息: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 - Add element after navigation element title don't works like I expect

matteradmin6PV0评论

I'm currently trying to add an empty <span> after every item title in my navigation which has childs. So only dropdown elements. Sadly also some of my non-dropdown items getting a <span> inserted after the title. This is so strange. What I'm doing wrong?

 add_filter( 'nav_menu_item_args', 'nav_menu_modify_dropdown', 10, 3 );
    function nav_menu_add_dropdown( $args, $item, $depth ) {
        error_log( print_r( $item, true ) );
        if ( $args->theme_location === 'primary-menu' && in_array( 'menu-item-has-children', $item->classes, true ) ) {
            $args->link_after = '<span></span>';
        }

        return $args;
    }

I'm currently trying to add an empty <span> after every item title in my navigation which has childs. So only dropdown elements. Sadly also some of my non-dropdown items getting a <span> inserted after the title. This is so strange. What I'm doing wrong?

 add_filter( 'nav_menu_item_args', 'nav_menu_modify_dropdown', 10, 3 );
    function nav_menu_add_dropdown( $args, $item, $depth ) {
        error_log( print_r( $item, true ) );
        if ( $args->theme_location === 'primary-menu' && in_array( 'menu-item-has-children', $item->classes, true ) ) {
            $args->link_after = '<span></span>';
        }

        return $args;
    }
Share Improve this question asked Mar 29, 2019 at 14:35 Johnny97Johnny97 2147 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

There is a mismatch of callback function names in your code. I believe, it's just a typo.

To understand the nature of your problem, we have to know, how walker-nav-menu uses its parameters, while traversing a menu tree. $args apply to entire menu tree. $item is an individual menu item.

Any changes to arguments in $args object, made via filters, are persistent for every next iteration of walker-nav-menu. Your code should be:

add_filter( 'nav_menu_item_args', 'nav_menu_modify_dropdown', 10, 2 );
function nav_menu_modify_dropdown( $args, $item ) {
    unset( $args->link_after );
    if ( $args->theme_location === 'primary-menu' && in_array( 'menu-item-has-children', $item->classes, true ) )
        $args->link_after = '<span></span>';
    return $args;
}

If there is only one menu location, you can simplify your conditional statement:

if ( in_array( 'menu-item-has-children', $item->classes, true ) )
    $args->link_after = '<span></span>';
Post a comment

comment list (0)

  1. No comments so far