未捕获的错误:不能将 WP_Post 类型的对象用作数组

Posted

技术标签:

【中文标题】未捕获的错误:不能将 WP_Post 类型的对象用作数组【英文标题】:Uncaught Error: Cannot use object of type WP_Post as array 【发布时间】:2017-12-14 13:35:49 【问题描述】:

我试图在我的模板文件中放置一个 WordPress 菜单。我发现使用 wp_nav_menu 不舒服,因为它输出的标记与我的静态 html 模板不兼容。我知道我可以使用自定义菜单 walker 类来修改 wp_nav_menu.xml 的标记。但相反,我只想使用菜单名称及其对应的 url 并将其放在我的模板中。为此,我使用this function 来获取菜单的所有属性/属性的数组。

这是这个函数生成的代码...

我通过使用 var_dump() 找到了它。 大批 ( [0] = WP_Post 对象 ( [ID] = 20 [post_author] = 1 [post_date] = 2017-07-10 11:50:32 [post_date_gmt] = 2017-07-10 10:50:32 [post_content] = [post_title] = 关于 [post_excerpt] = [post_status] = 发布 [comment_status] = 关闭 [ping_status] = 关闭 [post_password] = [post_name] = 关于 [to_ping] = [ping] = [post_modified] = 2017-07-10 11:50:40 [post_modified_gmt] = 2017-07-10 10:50:40 [post_content_filtered] = [post_parent] = 0 [guid] = http://localhost/mySite/?p=20 [菜单顺序] = 1 [post_type] = nav_menu_item [post_mime_type] = [comment_count] = 0 [过滤器] = 原始 [db_id] = 20 [menu_item_parent] = 0 [object_id] = 20 [对象] = 自定义 [类型] = 自定义 [type_label] = 自定义链接 [标题] = 关于 [网址] = # [目标] = [attr_title] = [描述] = [类] = 数组 ( [0] = ) [xfn] = ) [1] = WP_Post 对象 ( [身份证] = 21 [post_author] = 1 [post_date] = 2017-07-10 11:50:32 [post_date_gmt] = 2017-07-10 10:50:32 [post_content] = [post_title] = 我能做的事 [post_excerpt] = [post_status] = 发布 [comment_status] = 关闭 [ping_status] = 关闭 [post_password] = [post_name] = 可以做的事情 [to_ping] = [ping] = [post_modified] = 2017-07-10 11:50:40 [post_modified_gmt] = 2017-07-10 10:50:40 [post_content_filtered] = [post_parent] = 0 [guid] = http://localhost/mySite/?p=21 [菜单顺序] = 2 [post_type] = nav_menu_item [post_mime_type] = [comment_count] = 0 [过滤器] = 原始 [db_id] = 21 [menu_item_parent] = 0 [object_id] = 21 [对象] = 自定义 [类型] = 自定义 [type_label] = 自定义链接 [标题] = 我能做的事情 [网址] = # [目标] = [attr_title] = [描述] = [类] = 数组 ( [0] = ) [xfn] = ) [2] = WP_Post 对象 ( [ID] = 22 [post_author] = 1 [post_date] = 2017-07-10 11:50:32 [post_date_gmt] = 2017-07-10 10:50:32 [post_content] = [post_title] = 一些成就 [post_excerpt] = [post_status] = 发布 [comment_status] = 关闭 [ping_status] = 关闭 [post_password] = [post_name] = 一些成就 [to_ping] = [ping] = [post_modified] = 2017-07-10 11:50:40 [post_modified_gmt] = 2017-07-10 10:50:40 [post_content_filtered] = [post_parent] = 0 [guid] = http://localhost/mySite/?p=22 [菜单顺序] = 3 [post_type] = nav_menu_item [post_mime_type] = [comment_count] = 0 [过滤器] = 原始 [db_id] = 22 [menu_item_parent] = 0 [object_id] = 22 [对象] = 自定义 [类型] = 自定义 [type_label] = 自定义链接 [标题] = 一些成就 [网址] = # [目标] = [attr_title] = [描述] = [类] = 数组 ( [0] = ) [xfn] = ) [3] = WP_Post 对象 ( [ID] = 23 [post_author] = 1 [post_date] = 2017-07-10 11:50:32 [post_date_gmt] = 2017-07-10 10:50:32 [post_content] = [post_title] = 联系方式 [post_excerpt] = [post_status] = 发布 [comment_status] = 关闭 [ping_status] = 关闭 [post_password] = [post_name] = 联系人 [to_ping] = [ping] = [post_modified] = 2017-07-10 11:50:40 [post_modified_gmt] = 2017-07-10 10:50:40 [post_content_filtered] = [post_parent] = 0 [guid] = http://localhost/mySite/?p=23 [菜单顺序] = 4 [post_type] = nav_menu_item [post_mime_type] = [comment_count] = 0 [过滤器] = 原始 [db_id] = 23 [menu_item_parent] = 0 [object_id] = 23 [对象] = 自定义 [类型] = 自定义 [type_label] = 自定义链接 [标题] = 联系方式 [网址] = # [目标] = [attr_title] = [描述] = [类] = 数组 ( [0] = ) [xfn] = ) )

我的意图是获取'post_title'和'url'的值

我该怎么做?

我目前正在为这些代码行苦苦挣扎

function mt_get_menu_items($menu_name) 
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) 
    $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
    return wp_get_nav_menu_items($menu->term_id);


$menu_items = mt_get_menu_items ('main_menu');
<nav id="nav">
<ul>
    <?php if (isset ($menu_items) ) : ?>
    <?php foreach ( (array) $menu_items as $key => $menu_item ) : ?>
    <?php // $menu_item_array = get_object_vars($menu_item[$key]);
                    $menu_object = $menu_item[$key];
                    // this line is 98
                 ?>
    <li>
         // this line is 101
        <a href="<?php $menu_object->url; ?>" class="active"><?php $menu_object->post_title; ?></a>
    </li>
    <?php endforeach; ?>
    <?php endif; ?>
</ul>
</nav>

这会产生这个错误:

致命错误:未捕获错误:无法将 WP_Post 类型的对象用作 D:\xampp\htdocs\mySite\wp-content\themes\mySite\header.php:98 中的数组

【问题讨论】:

'Cannot use object of type stdClass as array' using Wordpress的可能重复 它是一个对象。使用-&gt; 而不是[] 另外,它可能不是来自 post_title 和 url。哪一行是 98? 恕我直言,请先阅读我的代码。我在从对象中检索数据时使用 ->。但它只是一直给我这个错误。 [解决我问题的代码] remove $menu_object = $menu_item[$key];总共在第 98 行。然后在第 101 行 .... 使用 echo $menu_item->url;回显菜单 url 使用 echo $menu_item->post_title;呼出菜单标题 【参考方案1】:

我将在此处发布此答案以供参考...

使用foreach 循环

foreach ($originalArray as $key => $data) 
  $post_date = $data->post_date; // 2017-07-10 11:50:32

因为要访问对象内部的数据,所以使用-&gt;

例如:$data['post_date']$data-&gt;post_date

【讨论】:

【参考方案2】:

您正在检索一个对象,您可以将其转换为数组,如下所示:

function object_to_array($obj) 
if(is_object($obj)) $obj = (array) $obj;
    if(is_array($obj)) 
        $new = array();
        foreach($obj as $key => $val) 
            $new[$key] = object_to_array($val);
        
    
    else $new = $obj;
    return $new;       

【讨论】:

以上是关于未捕获的错误:不能将 WP_Post 类型的对象用作数组的主要内容,如果未能解决你的问题,请参考以下文章

PHP致命错误:未捕获的错误:不能将stdClass类型的对象用作数组

致命错误:未捕获的错误:不能使用 PDOStatement 类型的对象作为数组

未捕获的错误:无法将 WP_Term 类型的对象用作数组

将 google 对象保存到 Core Data - 以 NSException 错误类型的未捕获异常终止

未捕获的错误:您不能将绑定多次应用于同一元素

为啥dojo会发出这个错误?未捕获的类型错误:对象 [对象对象] 没有方法“查询”