我正在研究一个儿童主题Walker_Nav_Menu
和Walker
上课引起了所有的骚动。我的父主题覆盖Walker_Nav_Menu
具体如下:
if ( ! class_exists( \'Zen_Menu_Walker\' ) ) {
class Zen_Menu_Walker extends Walker_Nav_Menu {
public function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) {
$element->has_children = ! empty( $children_elements[ $element->ID ] );
$element->classes[] = ( $element->current || $element->current_item_ancestor ) ? \'active\' : \'\';
$element->classes[] = ( $element->has_children && 1 !== $max_depth ) ? \'has-dropdown\' : \'\';
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
}
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$indent = ( $depth ) ? str_repeat( "\\t", $depth ) : \'\';
$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, $depth ) );
$class_names = $class_names ? \' class="\' . esc_attr( $class_names ) . \'"\' : \'\';
$id = apply_filters( \'nav_menu_item_id\', \'menu-item-\' . $item->ID, $item, $args, $depth );
$id = $id ? \' id="\' . esc_attr( $id ) . \'"\' : \'\';
$output .= $indent . \'<li\' . $id . $class_names . \'>\';
$atts = array();
$atts[\'title\'] = ! empty( $item->attr_title ) ? $item->attr_title : \'\';
$atts[\'target\'] = ! empty( $item->target ) ? $item->target : \'\';
$atts[\'rel\'] = ! empty( $item->xfn ) ? $item->xfn : \'\';
$atts[\'href\'] = ! empty( $item->url ) ? $item->url : \'\';
$atts = apply_filters( \'nav_menu_link_attributes\', $atts, $item, $args, $depth );
$attributes = \'\';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
//$value = ( \'href\' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= \' \' . $attr . \'="\' . $value . \'"\';
}
}
$item_output = $args->before;
$item_output .= \'<a\' . $attributes . \'>\';
/** This filter is documented in wp-includes/post-template.php */
$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 );
}
public function start_lvl( &$output, $depth = 0, $args = array() ) {
$output .= "\\n<ul class=\\"sub-menu dropdown\\">\\n";
}
}
}
这在父主题中没有任何致命错误。但在子主题中,它会产生致命错误:
Notice: Indirect modification of overloaded property WP_Post::$classes has no effect in /srv/www/htdocs/wpsite/wp-content/themes/zen-theme/class.menu.php on line 45
进行了一些挖掘
Stackoverflow 和
Google 我找到的唯一参考文献是:
SO网友:webtoure
如果“第45行”是以$element->classes[]
在您的display_element
方法,然后尝试以下操作:
public function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) {
$element->has_children = ! empty( $children_elements[ $element->ID ] );
if ( ! isset( $element->classes ) ) {
$element->classes = array();
}
$element->classes[] = ( $element->current || $element->current_item_ancestor ) ? \'active\' : \'\';
$element->classes[] = ( $element->has_children && 1 !== $max_depth ) ? \'has-dropdown\' : \'\';
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
}
问题可能是
display_element
函数接收的元素没有
classes
属性集,在这种情况下,您试图将数据推送到除该实例之外的任何位置都未引用的数组。
编辑:还有,为什么要使用display_element
当您可以将这些类插入start_el
自身被调用的函数display_element
将该元素传递给它?