我试着把我的逻辑变成这样:
内部调用以下代码
function start_el( &$output, $item, $depth, $args ) {
$this->sub_items = 0; // declared variable as private $sub_items = 0 above inside class
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
$menu_items = wp_get_nav_menu_items($menu->term_id);
foreach( $menu_items as $menu_item ) {
if( $menu_item->menu_item_parent == $item->ID ) {
$this->sub_items++;
}
}
}
}
我想每次$this->sub\\u items返回一级child的计算值时,我都可以在里面访问它
function start_lvl( &$output, $depth ) {
echo $this->sub_items;
}
但不幸的是,它不能正确输出:(