如果您查看WP_Nav_Menu_Widget
类,您将看到以下代码:
function widget($args, $instance) {
// Get menu
$nav_menu = ! empty( $instance[\'nav_menu\'] ) ? wp_get_nav_menu_object( $instance[\'nav_menu\'] ) : false;
if ( !$nav_menu )
return;
$instance[\'title\'] = apply_filters( \'widget_title\', empty( $instance[\'title\'] ) ? \'\' : $instance[\'title\'], $instance, $this->id_base );
echo $args[\'before_widget\'];
if ( !empty($instance[\'title\']) )
echo $args[\'before_title\'] . $instance[\'title\'] . $args[\'after_title\'];
wp_nav_menu( array( \'fallback_cb\' => \'\', \'menu\' => $nav_menu ) );
echo $args[\'after_widget\'];
}
这意味着没有任何机会勾选菜单。所以你需要看看
wp_nav_menu
函数实现,您可以在其中找到以下代码行:
$defaults = array(
\'menu\' => \'\',
\'container\' => \'div\',
\'container_class\' => \'\',
\'container_id\' => \'\',
\'menu_class\' => \'menu\',
\'menu_id\' => \'\',
\'echo\' => true,
\'fallback_cb\' => \'wp_page_menu\',
\'before\' => \'\',
\'after\' => \'\',
\'link_before\' => \'\',
\'link_after\' => \'\',
\'items_wrap\' => \'<ul id="%1$s" class="%2$s">%3$s</ul>\',
\'depth\' => 0,
\'walker\' => \'\',
\'theme_location\' => \'\'
);
$args = wp_parse_args( $args, $defaults );
$args = apply_filters( \'wp_nav_menu_args\', $args );
$args = (object) $args;
在这里,您可以看到传递给
wp_nav_menu
可以替换功能。所以你需要的是创建你自己的钩子处理器,它会将你的助行器添加到导航菜单中。它可以简单为:
function myplugin_custom_walker( $args ) {
return array_merge( $args, array(
\'walker\' => new My_Custom_Walker(),
// another setting go here ...
) );
}
add_filter( \'wp_nav_menu_args\', \'myplugin_custom_walker\' );