【发布时间】:2022-09-23 16:55:01
【问题描述】:
在本地机器上遇到问题print_r($args) 给我stdClass Object,在生产中我收到array。我检查 PHP 版本和 WordPress 版本,它们是相同的。什么会影响收到的格式?
我的代码
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ){ //li a span
$indent = ( $depth ) ? str_repeat(\"\\t\",$depth) : \'\';
$dir=get_template_directory_uri();
$li_attributes = \'\';
$class_names = $value = \'\';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
echo \'<pre>\';
print_r($args);
echo \'</pre>\';
$classes[] = ($args->walker->has_children) ? \'dropdown\' : \'\';
$classes[] = ($item->current || $item->current_item_ancestor) ? \'active\' : \'\';
$classes[] = \'menu-item-\' . $item->ID;
if( $depth && $args->walker->has_children ){
$classes[] = \'dropdown-submenu\';
}
$class_names = join(\' \', apply_filters(\'nav_menu_css_class\', array_filter( $classes ), $item, $args ) );
$class_names = \' class=\"\' . esc_attr($class_names) . \'\"\';
$id = apply_filters(\'nav_menu_item_id\', \'menu-item-\'.$item->ID, $item, $args);
$id = strlen( $id ) ? \' id=\"\' . esc_attr( $id ) . \'\"\' : \'\';
$output .= $indent . \'<li\' . $id . $value . $class_names . $li_attributes . \'>\';
$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) . \'\"\' : \'\';
$attributes .= ( $args->walker->has_children ) ? \' class=\"dropdown-toggle\" data-toggle=\"dropdown\"\' : \'\';
$item_output = $args->before;
$item_output .= \'<a\' . $attributes . \'>\';
$item_output .= $args->link_before . apply_filters( \'the_title\', $item->title, $item->ID ) . $args->link_after;
$dropdown_icons=\"\";
if ( $depth >= 0 && $args->walker->has_children ) {
$dropdown_icons=\' <span class=\"dropdown-toggle-button\">\';
if ($depth > 0) {
$dropdown_icons .= \'<svg class=\"angle-right\">
<use xlink:href=\"#angle-right-solid\"></use>
</svg>\';
}
$dropdown_icons .= \'</span></a>\'; //TODO INline 2 diff svg
}
else {
$dropdown_icons.= \'</a>\';
}
$item_output .= $dropdown_icons;
$item_output .= $args->after;
$output .= apply_filters ( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
}
更新 V1
产量
<pre>Array
(
[depth] => 0
[show_date] =>
[date_format] => F j, Y
[child_of] => 0
[exclude] =>
[title_li] =>
[echo] =>
[authors] =>
[sort_column] => menu_order, post_title
[link_before] =>
[link_after] =>
[item_spacing] => preserve
[walker] => Walker_Nav_Primary Object
(
[tree_type] => Array
(
[0] => post_type
[1] => taxonomy
[2] => custom
)
[db_fields] => Array
(
[parent] => menu_item_parent
[id] => db_id
)
[max_pages] => 1
[has_children] =>
)
[menu_id] => primary-menu
[menu_class] => menu
[container] => div
[before] =>
[after] =>
[menu] =>
[container_class] =>
[container_id] =>
[container_aria_label] =>
[fallback_cb] => wp_page_menu
[items_wrap] => <ul id=\"%1$s\" class=\"%2$s nav-menu\">%3$s</ul>
[theme_location] => menu-1
[hierarchical] => 0
[has_children] =>
)
</pre>
和错误Attempt to read property \"walker\" on array in
当地的
<pre>stdClass Object
(
[menu] => WP_Term Object
(
[term_id] => 2
[name] => Menu 1
[slug] => menu-1
[term_group] => 0
[term_taxonomy_id] => 2
[taxonomy] => nav_menu
[description] =>
[parent] => 0
[count] => 20
[filter] => raw
)
[container] => div
[container_class] =>
[container_id] =>
[container_aria_label] =>
[menu_class] => menu
[menu_id] => primary-menu
[echo] => 1
[fallback_cb] => wp_page_menu
[before] =>
[after] =>
[link_before] =>
[link_after] =>
[items_wrap] => <ul id=\"%1$s\" class=\"%2$s nav-menu\">%3$s</ul>
[item_spacing] => preserve
[depth] => 0
[walker] => Walker_Nav_Primary Object
(
[tree_type] => Array
(
[0] => post_type
[1] => taxonomy
[2] => custom
)
[db_fields] => Array
(
[parent] => menu_item_parent
[id] => db_id
)
[max_pages] => 1
[has_children] => 1
)
[theme_location] => menu-1
)
</pre>
我通过复制器复制整个网站,不更改菜单设置,只添加菜单链接
-
你能在这两种情况下发布
print_r的输出吗?我的直觉是,在本地你有一个菜单集,应该返回stdClass Object。但也许在 prod 中没有设置菜单并返回array,因为您将默认设置为array。您是否尝试过null作为默认值(这是 WP 文档显示的内容)并且仍然在 prod 中获得一个数组? -
@mikerojas 我更新了我的帖子(添加输出数据),我之前复制了整个网站