简体   繁体   中英

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!

您可以直接在wp_nav_menu中运行参数,而无需使用walker

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM