【问题标题】:Walker $args receive object on local and array on productionWalker $args 在本地接收对象并在生产中接收数组
【发布时间】: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] =&gt; 0
    [show_date] =&gt; 
    [date_format] =&gt; F j, Y
    [child_of] =&gt; 0
    [exclude] =&gt; 
    [title_li] =&gt; 
    [echo] =&gt; 
    [authors] =&gt; 
    [sort_column] =&gt; menu_order, post_title
    [link_before] =&gt; 
    [link_after] =&gt; 
    [item_spacing] =&gt; preserve
    [walker] =&gt; Walker_Nav_Primary Object
        (
            [tree_type] =&gt; Array
                (
                    [0] =&gt; post_type
                    [1] =&gt; taxonomy
                    [2] =&gt; custom
                )

            [db_fields] =&gt; Array
                (
                    [parent] =&gt; menu_item_parent
                    [id] =&gt; db_id
                )

            [max_pages] =&gt; 1
            [has_children] =&gt; 
        )

    [menu_id] =&gt; primary-menu
    [menu_class] =&gt; menu
    [container] =&gt; div
    [before] =&gt; 
    [after] =&gt; 
    [menu] =&gt; 
    [container_class] =&gt; 
    [container_id] =&gt; 
    [container_aria_label] =&gt; 
    [fallback_cb] =&gt; wp_page_menu
    [items_wrap] =&gt; <ul id=\"%1$s\" class=\"%2$s nav-menu\">%3$s</ul>
    [theme_location] =&gt; menu-1
    [hierarchical] =&gt; 0
    [has_children] =&gt; 
)
</pre>

和错误Attempt to read property \"walker\" on array in

当地的

<pre>stdClass Object
(
    [menu] =&gt; WP_Term Object
        (
            [term_id] =&gt; 2
            [name] =&gt; Menu 1
            [slug] =&gt; menu-1
            [term_group] =&gt; 0
            [term_taxonomy_id] =&gt; 2
            [taxonomy] =&gt; nav_menu
            [description] =&gt; 
            [parent] =&gt; 0
            [count] =&gt; 20
            [filter] =&gt; raw
        )

    [container] =&gt; div
    [container_class] =&gt; 
    [container_id] =&gt; 
    [container_aria_label] =&gt; 
    [menu_class] =&gt; menu
    [menu_id] =&gt; primary-menu
    [echo] =&gt; 1
    [fallback_cb] =&gt; wp_page_menu
    [before] =&gt; 
    [after] =&gt; 
    [link_before] =&gt; 
    [link_after] =&gt; 
    [items_wrap] =&gt; <ul id=\"%1$s\" class=\"%2$s nav-menu\">%3$s</ul>
    [item_spacing] =&gt; preserve
    [depth] =&gt; 0
    [walker] =&gt; Walker_Nav_Primary Object
        (
            [tree_type] =&gt; Array
                (
                    [0] =&gt; post_type
                    [1] =&gt; taxonomy
                    [2] =&gt; custom
                )

            [db_fields] =&gt; Array
                (
                    [parent] =&gt; menu_item_parent
                    [id] =&gt; db_id
                )

            [max_pages] =&gt; 1
            [has_children] =&gt; 1
        )

    [theme_location] =&gt; menu-1
)
</pre>

我通过复制器复制整个网站,不更改菜单设置,只添加菜单链接

  • 你能在这两种情况下发布print_r 的输出吗?我的直觉是,在本地你有一个菜单集,应该返回stdClass Object。但也许在 prod 中没有设置菜单并返回array,因为您将默认设置为array。您是否尝试过 null 作为默认值(这是 WP 文档显示的内容)并且仍然在 prod 中获得一个数组?
  • @mikerojas 我更新了我的帖子(添加输出数据),我之前复制了整个网站

标签: wordpress wp-nav-walker


【解决方案1】:

我遇到了同样的问题。问题是,在网站迁移期间,菜单不知何故丢失了他的“显示设置”(外观 -> 菜单 -> 我们正在谈论的菜单)。

所以请登录到迁移的 WP 并重置它。然后 Walker 将返回正确的对象而不是数组。

【讨论】:

    猜你喜欢
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    相关资源
    最近更新 更多