Yari Bernardus
Yari Bernardus

Reputation: 31

How to add <span> to wp_nav_menu in child theme

I can manage this in the wordpress files but it will be overwritten in the next update. How can i code this in the child theme functions or can i overwrite the wp_nav_menu.php file in the child theme?

/**
     * Filter a menu item's title.
     *
     * @since 4.4.0
     *
     * @param string $title The menu item's title.
     * @param object $item  The current menu item.
     * @param array  $args  An array of {@see wp_nav_menu()} arguments.
     * @param int    $depth Depth of menu item. Used for padding.
     */
    $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );

    $item_output = $args->before;
    $item_output .= '<a'. $attributes .'><span>';    //NOTE THE SPAN IS ADDED HERE
    $item_output .= $args->link_before . $title . $args->link_after;
    $item_output .= '</span></a>';        //AND HERE
    $item_output .= $args->after;

Thank you in advance!

Upvotes: 0

Views: 360

Answers (1)

Moishy
Moishy

Reputation: 3648

you can run an argument straight in to wp_nav_menu without using a walker

<?php wp_nav_menu( array('link_before' => '<span>', 'link_after' => '</span>')); ?>

Upvotes: 1

Related Questions