为什么$has\\u children在这里不起作用?
class walker_name extends Walker_Nav_Menu{
function start_el(&$output, $item, $depth, $args) {
$indent = ( $depth ) ? str_repeat( "\\t", $depth ) : \'\';
$class_names = $value = \'\';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names = join( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item ) );
$class_names = \' class="\' . esc_attr( $class_names ) . \'"\';
$output .= $indent . \'<li id="menu-item-\'. $item->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>\';
$has_children = (is_object($args) && $args->has_children) || (is_array($args) && $args[\'has_children\']);
if ( $has_children ) {
// not working
}
}
}
SO网友:solgar
我刚刚解决了这个问题!呜呜呜!问题是使用var_dump($args)
展示了很多这样的东西:
object(stdClass)#152 (16) { ["menu"]=> object(WP_Term)#145 (10) { ["term_id"]=>
int(2) ["name"]=> string(9) "Main menu" ["slug"]=> string(9) "main-menu"
["term_group"]=> int(0) ["term_taxonomy_id"]=> int(2) ["taxonomy"]=> string(8)
"nav_menu" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(12)
["filter"]=> string(3) "raw" } ["container"]=> string(0) "" ["container_class"]=>
string(0) "" ["container_id"]=> string(0) "" ["menu_class"]=> string(4) "menu"
["menu_id"]=> string(0) "" ["echo"]=> bool(true) ["fallback_cb"]=> string(12)
"wp_page_menu" ["before"]=> string(0) "" ["after"]=> string(0) "" ["link_before"]=>
string(0) "" ["link_after"]=> string(0) "" ["items_wrap"]=> string(4) "%3$s"
["depth"]=> int(0) ["walker"]=> object(themeslug_walker_nav_menu)#151 (4) {
["tree_type"]=> array(3) { [0]=> string(9) "post_type" [1]=> string(8) "taxonomy"
[2]=> string(6) "custom" } ["db_fields"]=> array(2) { ["parent"]=> string(16)
"menu_item_parent" ["id"]=> string(5) "db_id" } ["max_pages"]=> int(1)
["has_children"]=> bool(true) } ["theme_location"]=> string(0) "" }
在这个垃圾堆中搜索,您可以清楚地看到
["has_children"]=> bool(true)
但问题是它不是
$args
对象事实上,它是物体的一部分
"walker"
价值当您缩进这样的位输出时,这一点更加明显:
object(stdClass)#152 (16) {
["menu"]=> object(WP_Term)#145 (10) {
["term_id"]=> int(2)
["name"]=> string(9) "Main menu"
["slug"]=> string(9) "main-menu"
["term_group"]=> int(0)
["term_taxonomy_id"]=> int(2)
["taxonomy"]=> string(8) "nav_menu"
["description"]=> string(0) ""
["parent"]=> int(0)
["count"]=> int(12)
["filter"]=> string(3) "raw"
}
["container"]=> string(0) ""
["container_class"]=> string(0) ""
["container_id"]=> string(0) ""
["menu_class"]=> string(4) "menu"
["menu_id"]=> string(0) ""
["echo"]=> bool(true)
["fallback_cb"]=> string(12) "wp_page_menu"
["before"]=> string(0) ""
["after"]=> string(0) ""
["link_before"]=> string(0) ""
["link_after"]=> string(0) ""
["items_wrap"]=> string(4) "%3$s"
["depth"]=> int(0)
["walker"]=> object(themeslug_walker_nav_menu)#151 (4) {
["tree_type"]=> array(3) {
[0]=> string(9) "post_type"
[1]=> string(8) "taxonomy"
[2]=> string(6) "custom"
}
["db_fields"]=> array(2) {
["parent"]=> string(16) "menu_item_parent"
["id"]=> string(5) "db_id"
}
["max_pages"]=> int(1)
["has_children"]=> bool(true)
}
["theme_location"]=> string(0) ""
}
现在你可以清楚地看到这一点
has_children
需要调用此行的属性:
$args->walker->has_children