How to add a <span> tag to wp_nav_menu in a child theme

I’m creating a website with WordPress and Uncode theme, and I want to add a <span> tag to the primary menu in the child theme (in the ‘li’, after the ‘a’) and I don’t know how to do that.

I don’t want to modify the parent theme, just add the ‘array’ ‘after’ in the wp_nav_menu who has for theme_location, the value ‘primary’.

Do you have you any idea? Maybe with a ‘walker‘ (or other) in the functions.php of my child theme? I’m not an expert with PHP/WordPress codex, so do you have a solution?

Solutions Collecting From Web of "How to add a <span> tag to wp_nav_menu in a child theme"

The wp_nav_menu() function has a ton ( or bunch ) of parameters which makes customization fairly easy. I believe the one you’re specifically looking for is after which will add a passed string into the list after the link. An example would look like this:

wp_nav_menu( array(
    'menu'          => 'Main Menu',
    'container'     => 'nav',
    'theme_location'=> 'primary',
    'after'         => '<span></span>',
) );

And would output something like this:

<nav>
    <ul>
        <li><a href="#">Text</a><span></span></li>
    </ul>
</nav>

Alternatively, you could use the wp_nav_menu_args hook in functions.php to modify the arguments for the primary menu:

function modify_main_menu( $args ) {

    if( isset( $args['theme_location'] ) && 'primary' === $args['theme_location'] ) {
        $args['after'] = '<span></span>';
    }

    return $args;
}
add_filter( 'wp_nav_menu_args', 'modify_main_menu' );