我在WP Admin中定义了一个菜单,如下所示:
我希望每当我在父页面上时,都能够在侧栏上显示所有子链接。例如,如果用户在我的“关于我们”页面上,我希望以绿色突出显示的4个链接的列表显示在侧栏上。
我查看了wp_nav_menu()的文档,它似乎没有任何内置方法来指定给定菜单的特定节点以用作生成链接时的起点。
我创建了一种针对类似情况的解决方案,该解决方案依赖于页面父级创建的关系,但是我正在寻找一种专门使用菜单系统的解决方案。任何帮助,将不胜感激。
我在WP Admin中定义了一个菜单,如下所示:
我希望每当我在父页面上时,都能够在侧栏上显示所有子链接。例如,如果用户在我的“关于我们”页面上,我希望以绿色突出显示的4个链接的列表显示在侧栏上。
我查看了wp_nav_menu()的文档,它似乎没有任何内置方法来指定给定菜单的特定节点以用作生成链接时的起点。
我创建了一种针对类似情况的解决方案,该解决方案依赖于页面父级创建的关系,但是我正在寻找一种专门使用菜单系统的解决方案。任何帮助,将不胜感激。
Answers:
这仍然在我的脑海中,因此我重新审视并提出了这个解决方案,该解决方案并不太依赖上下文:
add_filter( 'wp_nav_menu_objects', 'submenu_limit', 10, 2 );
function submenu_limit( $items, $args ) {
if ( empty( $args->submenu ) ) {
return $items;
}
$ids = wp_filter_object_list( $items, array( 'title' => $args->submenu ), 'and', 'ID' );
$parent_id = array_pop( $ids );
$children = submenu_get_children_ids( $parent_id, $items );
foreach ( $items as $key => $item ) {
if ( ! in_array( $item->ID, $children ) ) {
unset( $items[$key] );
}
}
return $items;
}
function submenu_get_children_ids( $id, $items ) {
$ids = wp_filter_object_list( $items, array( 'menu_item_parent' => $id ), 'and', 'ID' );
foreach ( $ids as $id ) {
$ids = array_merge( $ids, submenu_get_children_ids( $id, $items ) );
}
return $ids;
}
$args = array(
'theme_location' => 'slug-of-the-menu', // the one used on register_nav_menus
'submenu' => 'About Us', // could be used __() for translations
);
wp_nav_menu( $args );
Walker_Nav_Menu
该类中子类化或使用过滤器。像所有菜单一样,太多东西无法评论-问新问题吗?
wp_filter_object_list
行wp_filter_object_list( $items, array( 'object_id' => $args->submenu ), 'and', 'ID' );
@goldenapples:您的Walker课堂无效。但是这个主意真的很好。我根据您的想法创建了一个助行器:
class Selective_Walker extends Walker_Nav_Menu
{
function walk( $elements, $max_depth) {
$args = array_slice(func_get_args(), 2);
$output = '';
if ($max_depth < -1) //invalid parameter
return $output;
if (empty($elements)) //nothing to walk
return $output;
$id_field = $this->db_fields['id'];
$parent_field = $this->db_fields['parent'];
// flat display
if ( -1 == $max_depth ) {
$empty_array = array();
foreach ( $elements as $e )
$this->display_element( $e, $empty_array, 1, 0, $args, $output );
return $output;
}
/*
* need to display in hierarchical order
* separate elements into two buckets: top level and children elements
* children_elements is two dimensional array, eg.
* children_elements[10][] contains all sub-elements whose parent is 10.
*/
$top_level_elements = array();
$children_elements = array();
foreach ( $elements as $e) {
if ( 0 == $e->$parent_field )
$top_level_elements[] = $e;
else
$children_elements[ $e->$parent_field ][] = $e;
}
/*
* when none of the elements is top level
* assume the first one must be root of the sub elements
*/
if ( empty($top_level_elements) ) {
$first = array_slice( $elements, 0, 1 );
$root = $first[0];
$top_level_elements = array();
$children_elements = array();
foreach ( $elements as $e) {
if ( $root->$parent_field == $e->$parent_field )
$top_level_elements[] = $e;
else
$children_elements[ $e->$parent_field ][] = $e;
}
}
$current_element_markers = array( 'current-menu-item', 'current-menu-parent', 'current-menu-ancestor' ); //added by continent7
foreach ( $top_level_elements as $e ){ //changed by continent7
// descend only on current tree
$descend_test = array_intersect( $current_element_markers, $e->classes );
if ( !empty( $descend_test ) )
$this->display_element( $e, $children_elements, 2, 0, $args, $output );
}
/*
* if we are displaying all levels, and remaining children_elements is not empty,
* then we got orphans, which should be displayed regardless
*/
/* removed by continent7
if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) {
$empty_array = array();
foreach ( $children_elements as $orphans )
foreach( $orphans as $op )
$this->display_element( $op, $empty_array, 1, 0, $args, $output );
}
*/
return $output;
}
}
现在您可以使用:
<?php wp_nav_menu(
array(
'theme_location'=>'test',
'walker'=>new Selective_Walker() )
); ?>
输出是一个包含当前根元素及其子元素(而不是其子元素)的列表。Def:根元素:=与当前页面相对应的顶级菜单项,或者是当前页面的父级或父级的父级...
这并不能完全回答最初的问题,但是几乎可以回答,因为仍然有顶层项目。这对我来说很好,因为我希望将顶级元素用作侧边栏的标题。如果要摆脱这种情况,则可能必须重写display_element或使用HTML解析器。
嗨@jessegavin:
导航菜单存储在自定义帖子类型和自定义分类中。每个菜单都存储为自定义分类法(即在中找到)的术语(即,在中找到“关于菜单”wp_terms
)。nav_menu
wp_term_taxonomy
每个导航菜单项目被定义为一个职位post_type=='nav_menu_item'
(即“关于公司”,发现wp_posts
)与它的存储作为后元属性(在wp_postmeta
使用)meta_key
的前缀_menu_item_*
哪里_menu_item_menu_item_parent
是你的菜单项的父导航菜单项后的ID。
菜单和菜单项之间的关系被存储在wp_term_relationships
其中object_id
涉及$post->ID
用于导航菜单项和$term_relationships->term_taxonomy_id
涉及在共同限定的菜单wp_term_taxonomy
和wp_terms
。
我非常确定,可以同时钩住这两个菜单'wp_update_nav_menu'
并'wp_update_nav_menu_item'
在其中创建实际菜单,wp_terms
并在其中建立一组平行的关系,wp_term_taxonomy
并且wp_term_relationships
每个具有子导航菜单项的导航菜单项也将变成它自己的导航菜单。
您还需要挂钩 'wp_get_nav_menus'
(我建议将其添加到WP 3.0中,这是基于我几个月前所做的一些类似工作),以确保您生成的Nav菜单不会显示给用户在admin中操作,否则会很快变得不同步,然后您将面临数据噩梦。
听起来像一个有趣的和有用的项目,但它是多一点点的代码和测试比我能买得起现在,以解决部分因为任何同步数据往往是一个PITA当谈到熨平所有的bug (因为:)但是,有了上述信息,我相当有动力,如果他们愿意,WordPress插件开发人员可以对其进行编码。
当然,您现在确实意识到,如果您编写代码,则必须将其发布回这里,以便我们都能从您的慷慨中受益!:-)
wp_nav_menu()
则需要克隆菜单,因为wp_nav_menu()
它与菜单结构紧密相关。另一种选择是复制wp_nav_menu()
代码并进行必要的修改以显示为子菜单。
这是一个walker扩展程序,它应该可以执行您想要的操作:
class Selective_Walker extends Walker_Nav_Menu
{
function walk( $elements, $max_depth) {
$args = array_slice(func_get_args(), 2);
$output = '';
if ($max_depth < -1) //invalid parameter
return $output;
if (empty($elements)) //nothing to walk
return $output;
$id_field = $this->db_fields['id'];
$parent_field = $this->db_fields['parent'];
// flat display
if ( -1 == $max_depth ) {
$empty_array = array();
foreach ( $elements as $e )
$this->display_element( $e, $empty_array, 1, 0, $args, $output );
return $output;
}
/*
* need to display in hierarchical order
* separate elements into two buckets: top level and children elements
* children_elements is two dimensional array, eg.
* children_elements[10][] contains all sub-elements whose parent is 10.
*/
$top_level_elements = array();
$children_elements = array();
foreach ( $elements as $e) {
if ( 0 == $e->$parent_field )
$top_level_elements[] = $e;
else
$children_elements[ $e->$parent_field ][] = $e;
}
/*
* when none of the elements is top level
* assume the first one must be root of the sub elements
*/
if ( empty($top_level_elements) ) {
$first = array_slice( $elements, 0, 1 );
$root = $first[0];
$top_level_elements = array();
$children_elements = array();
foreach ( $elements as $e) {
if ( $root->$parent_field == $e->$parent_field )
$top_level_elements[] = $e;
else
$children_elements[ $e->$parent_field ][] = $e;
}
}
$current_element_markers = array( 'current-menu-item', 'current-menu-parent', 'current-menu-ancestor' );
foreach ( $top_level_elements as $e ) {
// descend only on current tree
$descend_test = array_intersect( $current_element_markers, $e->classes );
if ( empty( $descend_test ) ) unset ( $children_elements );
$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
}
/*
* if we are displaying all levels, and remaining children_elements is not empty,
* then we got orphans, which should be displayed regardless
*/
if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) {
$empty_array = array();
foreach ( $children_elements as $orphans )
foreach( $orphans as $op )
$this->display_element( $op, $empty_array, 1, 0, $args, $output );
}
return $output;
}
}
宽松地基于我先前在评论中引用的mfields代码。它所做的只是在遍历菜单时检查是否当前元素是(1)当前菜单项,或(2)当前菜单项的祖先,并且仅当以下任一条件为真时,才展开其下方的子树。希望这对您有用。
要使用它,只需在调用菜单时添加一个“ walker”参数,即:
<?php wp_nav_menu(
array(
'theme_location'=>'test',
'walker'=>new Selective_Walker() )
); ?>
$top_level_elements
并在调用之前添加自己的测试即可$this->display_element
。
depth
参数给wp_nav_menu
?
更新: 我把它变成了一个插件。在这里下载。
我需要自己解决这个问题,最终要为菜单查找的结果写一个过滤器。它使您可以wp_nav_menu
照常使用,但可以根据父元素的标题选择菜单的子部分。将submenu
参数添加到菜单,如下所示:
wp_nav_menu(array(
'menu' => 'header',
'submenu' => 'About Us',
));
您甚至可以通过在斜杠中加几个深度:
wp_nav_menu(array(
'menu' => 'header',
'submenu' => 'About Us/Board of Directors'
));
或者,如果您更喜欢使用数组:
wp_nav_menu(array(
'menu' => 'header',
'submenu' => array('About Us', 'Board of Directors')
));
它使用标题的子标题版本,这应该使它宽恕诸如大写和标点符号之类的内容。
$loc = split( "/", $loc );
为$loc = preg_split( "~/~", $loc );
我为自己安排了以下课程。它将找到当前页面的顶级导航父级,或者您可以在walker构造函数中为其指定目标顶级导航ID。
class Walker_SubNav_Menu extends Walker_Nav_Menu {
var $target_id = false;
function __construct($target_id = false) {
$this->target_id = $target_id;
}
function walk($items, $depth) {
$args = array_slice(func_get_args(), 2);
$args = $args[0];
$parent_field = $this->db_fields['parent'];
$target_id = $this->target_id;
$filtered_items = array();
// if the parent is not set, set it based on the post
if (!$target_id) {
global $post;
foreach ($items as $item) {
if ($item->object_id == $post->ID) {
$target_id = $item->ID;
}
}
}
// if there isn't a parent, do a regular menu
if (!$target_id) return parent::walk($items, $depth, $args);
// get the top nav item
$target_id = $this->top_level_id($items, $target_id);
// only include items under the parent
foreach ($items as $item) {
if (!$item->$parent_field) continue;
$item_id = $this->top_level_id($items, $item->ID);
if ($item_id == $target_id) {
$filtered_items[] = $item;
}
}
return parent::walk($filtered_items, $depth, $args);
}
// gets the top level ID for an item ID
function top_level_id($items, $item_id) {
$parent_field = $this->db_fields['parent'];
$parents = array();
foreach ($items as $item) {
if ($item->$parent_field) {
$parents[$item->ID] = $item->$parent_field;
}
}
// find the top level item
while (array_key_exists($item_id, $parents)) {
$item_id = $parents[$item_id];
}
return $item_id;
}
}
导航电话:
wp_nav_menu(array(
'theme_location' => 'main_menu',
'walker' => new Walker_SubNav_Menu(22), // with ID
));
@davidn @hakre嗨,我有一个丑陋的解决方案,没有HTML解析器或覆盖display_element。
class Selective_Walker extends Walker_Nav_Menu
{
function walk( $elements, $max_depth) {
$args = array_slice(func_get_args(), 2);
$output = '';
if ($max_depth < -1) //invalid parameter
return $output;
if (empty($elements)) //nothing to walk
return $output;
$id_field = $this->db_fields['id'];
$parent_field = $this->db_fields['parent'];
// flat display
if ( -1 == $max_depth ) {
$empty_array = array();
foreach ( $elements as $e )
$this->display_element( $e, $empty_array, 1, 0, $args, $output );
return $output;
}
/*
* need to display in hierarchical order
* separate elements into two buckets: top level and children elements
* children_elements is two dimensional array, eg.
* children_elements[10][] contains all sub-elements whose parent is 10.
*/
$top_level_elements = array();
$children_elements = array();
foreach ( $elements as $e) {
if ( 0 == $e->$parent_field )
$top_level_elements[] = $e;
else
$children_elements[ $e->$parent_field ][] = $e;
}
/*
* when none of the elements is top level
* assume the first one must be root of the sub elements
*/
if ( empty($top_level_elements) ) {
$first = array_slice( $elements, 0, 1 );
$root = $first[0];
$top_level_elements = array();
$children_elements = array();
foreach ( $elements as $e) {
if ( $root->$parent_field == $e->$parent_field )
$top_level_elements[] = $e;
else
$children_elements[ $e->$parent_field ][] = $e;
}
}
$current_element_markers = array( 'current-menu-item', 'current-menu-parent', 'current-menu-ancestor' ); //added by continent7
foreach ( $top_level_elements as $e ){ //changed by continent7
// descend only on current tree
$descend_test = array_intersect( $current_element_markers, $e->classes );
if ( !empty( $descend_test ) )
$this->display_element( $e, $children_elements, 2, 0, $args, $output );
}
/*
* if we are displaying all levels, and remaining children_elements is not empty,
* then we got orphans, which should be displayed regardless
*/
/* removed by continent7
if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) {
$empty_array = array();
foreach ( $children_elements as $orphans )
foreach( $orphans as $op )
$this->display_element( $op, $empty_array, 1, 0, $args, $output );
}
*/
/*added by alpguneysel */
$pos = strpos($output, '<a');
$pos2 = strpos($output, 'a>');
$topper= substr($output, 0, $pos).substr($output, $pos2+2);
$pos3 = strpos($topper, '>');
$lasst=substr($topper, $pos3+1);
$submenu= substr($lasst, 0, -6);
return $submenu;
}
}
导航菜单输出包括当前项目,当前项目祖先等的许多类。在某些情况下,我可以通过让整个导航树输出然后使用css将其缩减为以下内容来完成您想做的事情仅当前页面的子级,等等。
我做了一个改良的助行器,应该有所帮助!并不完美-它留下了一些空元素,但是可以解决问题。修改基本上是那些$ current_branch位。希望它能对某人有所帮助!
class Kanec_Walker_Nav_Menu extends Walker {
/**
* @see Walker::$tree_type
* @since 3.0.0
* @var string
*/
var $tree_type = array( 'post_type', 'taxonomy', 'custom' );
/**
* @see Walker::$db_fields
* @since 3.0.0
* @todo Decouple this.
* @var array
*/
var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
/**
* @see Walker::start_lvl()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of page. Used for padding.
*/
function start_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class=\"sub-menu\">\n";
}
/**
* @see Walker::end_lvl()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of page. Used for padding.
*/
function end_lvl(&$output, $depth) {
global $current_branch;
if ($depth == 0) $current_branch = false;
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
}
/**
* @see Walker::start_el()
* @since 3.0.0
*
* @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. Used for padding.
* @param int $current_page Menu item ID.
* @param object $args
*/
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
global $current_branch;
// Is this menu item in the current branch?
if(in_array('current-menu-ancestor',$item->classes) ||
in_array('current-menu-parent',$item->classes) ||
in_array('current-menu-item',$item->classes)) {
$current_branch = true;
}
if($current_branch && $depth > 0) {
$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 ) );
$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="' . esc_attr( $item->url ) .'"' : '';
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
/**
* @see Walker::end_el()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Page data object. Not used.
* @param int $depth Depth of page. Not Used.
*/
function end_el(&$output, $item, $depth) {
global $current_branch;
if($current_branch && $depth > 0) $output .= "</li>\n";
if($depth == 0) $current_branch = 0;
}
}
在我的插件中查看代码或将其用于您的目的;)
该插件添加了增强的“导航菜单”小部件。它提供了许多选项,可以设置这些选项以通过小部件自定义自定义菜单的输出。
功能包括: