问题描述
我尝试寻找有关如何从自定义菜单中排除/删除导航菜单项的信息,而我发现唯一的 thread 没有任何对我有用的答案。
背景:
我在我的网站上使用 WP 自定义菜单 (wp_nav_menu) 和 jqDock 组合了一个 Dock 菜单。由于 jqDock 需要连续的图像或图像链接才能发挥作用,所以我使用自定义步行器,因此导航菜单 HTML 输出看起来像这样:
<div id="menu-first" class="nav">
<a><img src="http://path/to/image-1.png"/></a>
<a><img src="http://path/to/image-2.png"/></a>
<a><img src="http://path/to/image-3.png"/></a>
etc...
</div>
我的自定义步行者的代码是:
class custom_nav_walker extends Walker_Nav_Menu
{
var $tree_type = array( 'post_type', 'taxonomy', 'custom' );
var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
function start_lvl(&$output, $depth) {
$indent = str_repeat("t", $depth);
$output .= "n$indent<ul class="sub-menu">n";
}
function end_lvl(&$output, $depth) {
$indent = str_repeat("t", $depth);
$output .= "$indent</ul>n";
}
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "t", $depth ) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
$class_names = ' class="' . esc_attr( $class_names ) . '"';
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
//$output .= $indent . '<li' . $id . $value . $class_names .'>';
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="'%20%20%20.%20esc_attr(%20$item->url%20%20%20%20%20%20%20%20)%20.'"' : '';
$description = ! empty( $item->description ) ? esc_attr( strtolower( $item->description )) : '';
$item_title = ! empty( $item->attr_title ) ? esc_attr( $item->attr_title ) : '';
if ( strpos($description, ';') !== false ) {
$description_array = explode (';', $description);
$image_name = $description_array[0];
$image_alt = $description_array[1];
} else {
$image_name = $description;
$image_alt = $item_title;
}
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before .'<img src="'.get_bloginfo('template_url').'/images/skin1/'.$image_name.'" alt="'.$image_alt.'" title="'.$item_title.'" />'.$args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
function end_el(&$output, $item, $depth) {
$output .= "";
}
}
然后,jqDock 脚本捕获菜单 ID(‘menu-first’),并使用 Dock 菜单替换 wp_nav_menu 输出。 Dock 菜单的 HTML 输出根据加载 jqDock 时指定的选项而更改。
问题:
我不想根据用户在网站上的位置显示 (即排除) 某些菜单项。例如,当用户不在家中时,我只想显示 「家庭」 项目,只有当他在时,才显示 「随机」 邮件项目。
丢弃的解决方案:
一个。多种菜单:注册和创建多个菜单,然后有条件地调用它们可以工作; 不过,由于很多原因,我认为这不是理想的,也不是一个干净的解决方案。此外,多个菜单不容易维护或更新。
湾正则表达式搜索和替换:这可能会迫使我每次更改 jqDock 选项时更改针参数,因为 HTML 输出被修改。
C 。 CSS ‘display’ 属性:通过 CSS 显示属性隐藏项目的作用,但由于它必须应用于 jqDock 菜单输出,它会影响菜单的可视化渲染。
4. 解决方案失败
一个。过滤到 wp_nav_menu_items:我尝试捕获’$items’ 变量 (字符串),并通过条件标记为以下代码分配不同的值:
function userf_dynamic_nav_menu ($items) {
$items_array_home = explode('<a', $items);
$items_array_nothome = $items_array_home;
unset($items_array_home[1]);
unset($items_array_nothome[2]);
$items_home = implode('<a', $items_array_home);
$items_nothome = implode('<a', $items_array_nothome);
if ( is_home() ) {
$items = $items_home;
} else {
$items = $items_nothome;
}
return $items;
}
add_filter('wp_nav_menu_first_items', 'userf_dynamic_nav_menu');
这只能部分工作,因为菜单项确实改变,但条件标签被忽略。我想这是有道理的,因为应用过滤器的时刻。
湾自定义导航菜单功能:我尝试创建自己的自定义导航菜单功能,以便可以向 $ defaults 数组添加一个 exclude 参数,并使用 wp_list_pages
中稍作修改的代码填充附加参数:
$exclude_array = ( $args->exclude ) ? explode(',', $args->exclude) : array();
$args->exclude = implode( ',', apply_filters('wp_nav_menu_excludes', $exclude_array) );
有任何想法吗?
最佳解决方案
方法 1
您可以向自定义沃克添加一个构造函数来存储一些其他的排除参数,如:
class custom_nav_walker extends Walker_Nav_Menu {
function __construct( $exclude = null ) {
$this->exclude = $exclude;
}
function skip( $item ) {
return in_array($item->ID, (array)$this->exclude);
// or
return in_array($item->title, (array)$this->exclude);
// etc.
}
// ...inside start_el, end_el
if ( $this->skip( $item ) ) return;
}
或者放下构造函数并设置其 $exclude
属性,然后将其作为步行者传递给 wp_nav_menu()
,如下所示:
$my_custom_nav_walker = new custom_nav_walker;
$my_custom_nav_walker->exclude = array( ... );
根据您排除的内容,将正确的表单提供给排除。
方法 2
这是通过挂入 wp_get_nav_menu_items
过滤器来做到这一点。
function wpse31748_exclude_menu_items( $items, $menu, $args ) {
// Iterate over the items to search and destroy
foreach ( $items as $key => $item ) {
if ( $item->object_id == 168 ) unset( $items[$key] );
}
return $items;
}
add_filter( 'wp_get_nav_menu_items', 'wpse31748_exclude_menu_items', null, 3 );
注意:object_id
是菜单指向的对象,而 ID
是菜单 ID,这些都不同。
让我知道你的想法。
参考文献
注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。