Wrapping my <li>'s with <a>

时间:2016-03-10 作者:RLM

更改wp菜单生成的输出的最简单方法是什么<li><a href="">nav link</a></li><a href=""><li><li/></a>? 是否在中创建数组functions.php?

这就是我的functions.php

function register_my_menus() {
    register_nav_menus(
        array(
            \'primary\' => __( \'Main Menu\' ),
            \'mobile-menu\' => __( \'Mobile Menu\' ),
        )
    );
}
add_action( \'init\', \'register_my_menus\' );

3 个回复
SO网友:Peter Thomas Simmons

我不建议在ul和li之间做任何事情。

如果使锚定标记显示:块填充列表,则可以单击整个列表标记。因此,不要在列表中添加任何高度、宽度或填充,而是使用锚来管理所有内容。

a {
     display: block;
     width: 100%;
}

SO网友:RLM

我真的只是想<li> linkable和我(简单地)通过去掉menu li 并将其添加到display:blockmenu li a

SO网友:Madan Karki

使用"T5_Nav_Menu_Walker_Simple" 用于修改标记的自定义walkerWp_nav_menu:

<?php # -*- coding: utf-8 -*-
/**
 * Create a nav menu with very basic markup.
 *
 * @author Thomas Scholz http://toscho.de
 * @version 1.0
 */
class T5_Nav_Menu_Walker_Simple extends Walker_Nav_Menu
{
    /**
     * Start the element output.
     *
     * @param  string $output Passed by reference. Used to append additional content.
     * @param  object $item   Menu item data object.
     * @param  int $depth     Depth of menu item. May be used for padding.
     * @param  array $args    Additional strings.
     * @return void
     */
    public function start_el( &$output, $item, $depth, $args )
    {
        $output     .= \'<li>\';
        $attributes  = \'\';
        ! empty ( $item->attr_title )
            // Avoid redundant titles
            and $item->attr_title !== $item->title
            and $attributes .= \' title="\' . esc_attr( $item->attr_title ) .\'"\';
        ! empty ( $item->url )
            and $attributes .= \' href="\' . esc_attr( $item->url ) .\'"\';
        $attributes  = trim( $attributes );
        $title       = apply_filters( \'the_title\', $item->title, $item->ID );
        $item_output = "$args->before<a $attributes>$args->link_before$title</a>"
                        . "$args->link_after$args->after";
        // Since $output is called by reference we don\'t need to return anything.
        $output .= apply_filters(
            \'walker_nav_menu_start_el\'
            ,   $item_output
            ,   $item
            ,   $depth
            ,   $args
        );
    }
    /**
     * @see Walker::start_lvl()
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @return void
     */
    public function start_lvl( &$output )
    {
        $output .= \'<ul class="sub-menu">\';
    }
    /**
     * @see Walker::end_lvl()
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @return void
     */
    public function end_lvl( &$output )
    {
        $output .= \'</ul>\';
    }
    /**
     * @see Walker::end_el()
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @return void
     */
    function end_el( &$output )
    {
        $output .= \'</li>\';
    }
}

相关推荐

Disable automatic embed links

在当前的wordpress版本(5.2..)上有什么方法吗,要打开自动嵌入链接(wp embed.min.js),这会将url转换为iframe吗?我尝试过插件、代码片段和函数。php。。。很明显,对于wp的最新版本,它们都不再有效。