添加后wp-custom-menu-filter 插件-为了隐藏未登录用户的某些菜单项,当前的\\u page\\u item类从我的菜单中消失。$item->classes
在walker中是一个空数组,而不是通常的page-item-$id
, current_page_item
.
我已经查看了源代码,但它没有修改classes数组,所以我不知道发生了什么。下面是代码的相关部分。
function wps_custom_nav_menu_items( $args ){
global $wpcmfp_settings;
$nav_item_db_id = $args[1]->ID;
$nav_menu = wp_get_object_terms( $nav_item_db_id, \'nav_menu\' );
$menu_id = $nav_menu[0]->term_id;
// Get the class to exclude
if ( !is_user_logged_in() ) {
$exclusion_class = $wpcmfp_settings[ \'menu-\' . $menu_id . \'-loggedout\' ];
}
else {
$exclusion_class = $wpcmfp_settings[ \'menu-\' . $menu_id . \'-loggedin\' ];
}
$nav_items = wp_get_nav_menu_items( $menu_id );
// Cycle through all nav_items
foreach ( $nav_items as $nav_item ) {
// Cycle through all classes
for ( $i=0; $i < count( $nav_item->classes ); $i++ ) {
$exclude = false;
// if nothing is there set to add
if ( strlen ( $nav_item->classes[ $i ] ) < 1 ) {
$exclude = false;
}
else
{
// if matches add to exclusion array & break loop
if ( $nav_item->classes[ $i ] == $exclusion_class ) {
$excluded_nav_items[] = $nav_item;
$exclude = true;
break;
}
}
}
if ( $exclude != true )
$modified_nav_items[] = $nav_item;
}
return $modified_nav_items;
}
add_filter( \'wp_nav_menu_objects\', \'wps_custom_nav_menu_items\' , 10 , 3);
我使用的是自定义页面漫游器,我尝试过使用默认页面漫游器,但没有任何效果,没有这个插件,自定义页面漫游器就可以正常工作。以下是步行者的密码:
class My_Walker_Page extends Walker_Nav_Menu {
protected $_counter = 0;
protected $_itemsPerColumn = 1;
function start_lvl(&$output, $depth) {
$this->_counter = 0;
$output .= \'<some html>\';
}
function end_lvl(&$output, $depth) {
$output .= \'<some html>\';
$this->_counter = 0;
}
function start_el(&$output, $page, $depth, $args, $current_page = null) {
$args = ( array ) $args;
if ($depth == 0) {
return parent::start_el ( $output, $page, $depth, $args, $current_page );
}
if ($depth == 1) {
extract ( $args, EXTR_SKIP );
$output .= \'<dt><a href="\' . esc_attr ( $page->url ) . \'" title="\' . esc_attr ( wp_strip_all_tags ( apply_filters ( \'the_title\', $page->title, $page->ID ) ) ) . \'">\' . $link_before . apply_filters ( \'the_title\', $page->title, $page->ID ) . $link_after . \'</a>\';
if (! empty ( $page->description )) {
$output .= \'<span class="menu_item_description">\' . $page->description . \'</span>\';
}
$output .= \'</dt>\';
$this->_counter ++;
if ($this->_counter % $this->_itemsPerColumn == 0) {
$output .= \'</dl><dl>\';
}
}
}
function end_el(&$output, $page, $depth) {
if ($depth == 0) {
return parent::end_el ( &$output, $page, $depth );
}
}
function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output) {
if (! $element)
return;
$id_field = $this->db_fields [\'id\'];
if ($depth == 0) {
$this->_itemsPerColumn = ceil ( count ( $children_elements [$element->$id_field] ) / 4 );
}
return parent::display_element ( $element, $children_elements, $max_depth, $depth, $args, $output );
}
}
我同意用另一种方法来隐藏未登录用户的菜单项,而不是修复插件,但由于我从未使用
wp_nav_menu_objects
我真的不知道发生了什么事。