$args->Has_Child不使用start_el()

时间:2015-05-28 作者:Ahmed Saad

为什么$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
            }
    }
}

2 个回复
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

SO网友:s_ha_dum

我不知道你为什么这么想$args->has_children (或$args[\'has_children\']) 根本不存在。我在测试中没有发现nor do I see it in source.

我想你想要的是$menu_item->menu_item_parentseen in the Core walker.

330         $sorted_menu_items = $menu_items_with_children = array();
331         foreach ( (array) $menu_items as $menu_item ) {
332                 $sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
333                 if ( $menu_item->menu_item_parent )
334                         $menu_items_with_children[ $menu_item->menu_item_parent ] = true;
335         }

结束

相关推荐

Mega Menu Walker

我正在尝试创建一个超大菜单漫游器。不幸的是,walkers完全没有掌握我的编码知识。我真的需要一些帮助才能让它工作。以下是我需要的功能:包裹第二层<ul> 在里面<section>. [COMPLETE]</当用户在<li> 在第二级<ul>, 做那个<li> 开始一个新的<ul>. 如果是第一次<li> 在列表中,不要做任何事情,以防止形成空的无序列表。[COMPLETE]</当用户在<li> 在