使用自定义查看器输出现有wp_NAV_MENU

时间:2018-03-11 作者:fightstarr20

如果现有的wp\\u nav\\u菜单项有这样的特定类,我将尝试输出另一个wp\\u nav\\u菜单。。

/* Custom Walker */
class IBenic_Walker extends Walker_Nav_Menu {

// Displays start of an element. E.g \'<li> Item Name\'
// @see Walker::start_el()
function start_el(&$output, $item, $depth=0, $args=array(), $id = 0) {
    $object = $item->object;
    $type = $item->type;
    $title = $item->title;
    $description = $item->description;
    $permalink = $item->url;

    $output .= "<li class=\'" .  implode(" ", $item->classes) . "\'>";
    $output .= \'<a href="\' . $permalink . \'">\';
    $output .= $title;
    $output .= \'</a>\';

    /* If Item Class Is shop_nav */
    if (in_array("shop_nav", $item->classes)) {
        $output .= \'<ul class="sub-menu">
                        \' . wp_nav_menu( array(
                                \'theme_location\' => \'col_1\',
                                \'menu_id\'        => \'col_1\',
                                \'menu_class\'     => \'col_1\',
                                \'container\'      => \'\',     
                                ) ) . \'
                    </ul>\';
    }

    $output .= \'</li>\';

}
}
这并没有像预期的那样起作用,有人能看出我错在哪里吗?

1 个回复
SO网友:obiPlabon

默认情况下wp_nav_menu() 输出导航标记。如果要存储它或要将其与其他字符串连接,则必须设置echo 参数到false (默认情况下true).

$output .= \'<ul class="sub-menu">
        \' . wp_nav_menu( array(
                \'theme_location\' => \'col_1\',
                \'menu_id\'        => \'col_1\',
                \'menu_class\'     => \'col_1\',
                \'container\'      => \'\',
                \'echo\'           => false,
                ) ) . \'
        </ul>\';

结束