我正在处理一个项目,我希望wp\\u nav\\u菜单为当前页面、当前祖先输出其他css类,并删除其他内容,如页面id和menu-id。
我已经做了一些工作,比如使用自定义助行器删除不必要的类和id:s。但我无法让当前的\\u祖先更改为其他类。
以下是我到目前为止的想法:
class Clean_Walker extends Walker_Nav_Menu
{
/**
* @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 = 0, $args = array(), $id = 0 )
{
global $wp_query;
$class_names = $value = \'\';
$classes[] = ( $item->current ) ? \'sel\' : \'\';
// $current_classes = array(
// \'current-menu-item\',
// \'current-menu-parent\',
// \'current-menu-ancestor\',
// \'current_page_item\',
// \'current_page_parent\',
// \'current_page_ancestor\'
// );
// foreach ($current_classes as $current_class )
// {
// if ( in_array( $current_class, $classes ) )
// {
// $classes[] = "active";
// break;
// }
// }
$class_names = join( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item, $args ) );
$class_names = $class_names ? \' class="\' . esc_attr( $class_names ) . \'"\' : \'\';
$output .= \'<li\'. $class_names .\'>\';
$attributes = ! empty( $item->attr_title ) ? \' title="\' . esc_attr( $item->title .\' \'. $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 ) .\'"\' : \'\';
$attribute = ! empty( $item->attr_title ) ? \'<span>\' . esc_attr( $item->attr_title ) . \'</span>\' : \'\';
$item_output = \'<a\'. $attributes .\'>\';
$item_output .= $args->link_before . apply_filters( \'the_title\', $item->title, $item->ID );
$item_output .= $attribute . $args->link_after;
$item_output .= \'</a>\';
$output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
}
}
我还需要属性字段位于范围内,以便进行一些样式设置。所以问题是我需要将“current\\u祖先”更改为类
sel
.
类sel正在“单页”上工作,所以唯一的问题是当im更深一层时。
我已经搜索了WPSE,但运气不好。谢谢
SO网友:Max Yudin
删除ID和类,除了$current_indicators
大堆您可以在那里轻松地添加/删除类。然后应用@Simon的过滤器。
// based on http://snipplr.com/view/58549/
class Clnr_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_el(&$output, $item, $depth, $args) {
//~ pre($item);
global $wp_query;
$indent = ($depth) ? str_repeat(\'\\t\',$depth) : \'\';
$class_names = \'\';
$classes = empty($item->classes) ? array() : (array) $item->classes;
$current_indicators = array(
\'current-menu-item\',
\'current-menu-parent\',
\'current-menu-ancestor\',
\'current_page_item\',
\'current_page_parent\',
\'current-page-ancestor\' // see Feb 20 update below
);
$newClasses = array();
foreach($classes as $el) {
if( in_array($el, $current_indicators) ) {
array_push($newClasses, $el);
}
}
$class_names = join( \' \', apply_filters(\'nav_menu_css_class\', array_filter($newClasses), $item) );
$output .= $indent . \'<li class="\' . $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) . \'"\' : \'\';
if($depth != 0) {
//children stuff, maybe you\'d like to store the submenu\'s somewhere?
}
$item_output = $args->before;
$item_output .= \'<a\' . $attributes . \'>\';
$item_output .= $args->link_before . apply_filters(\'the_title\', $item->title, $item->ID);
$item_output .= \'</a>\';
$item_output .= $args->after;
$output .= apply_filters(\'walker_nav_menu_start_el\', $item_output, $item, $depth, $args);
}
}
Updated (February 20):
Wordpress 3.5.1(至少)中似乎有一个bug。没有这样的类current_page_ancestor
(下划线分隔)如果current-page-ancestor
(破折号分开)取而代之。我现在没有时间去找出原因,但看起来是_wp_menu_item_classes_by_context()
在…内/wp-includes/nav-menu-template.php
.