1

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?

  • Do you like your menu item will be like <li><a href="#">Home <span></span></a></li> ? – webHasan Aug 12 '16 at 10:52
  • @webHasan Yes. Or like this: <li><a href="#">Home</a><span></span></li>. Both solutions are right for me. – Jérémy Debray Aug 12 '16 at 11:08
  • If <li><a href="#">Home <span></span></a></li> is your solution then you can use <span></span> as Navigation Label after your menu text. See screenshot. prntscr.com/c4ssqm – webHasan Aug 12 '16 at 11:32
  • @webHasan I know this tip, but it's not a good solution in my case because the Navigation Label is used in others parts of theme (like 'title' and 'data-title' of parents 'li'). Do you have an other solution? – Jérémy Debray Aug 12 '16 at 13:08
3

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' );
| improve this answer | |
  • how to change this code that replace a to span for elements who has sub menu? – voronovam Jul 6 '17 at 2:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.