当前位置: 首页 > 知识库问答 >
问题:

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

吴正祥
2023-03-14

我试图在模板文件中放置WordPress菜单。我发现使用wp_nav_菜单不舒服,因为它输出的标记与我的静态HTML模板不兼容。我知道我可以使用定制的menu walker类来修改wp_nav_菜单的标记。但是,我只想使用菜单名及其对应的url,并将其放在我的模板中。为此,我使用此函数获取菜单的所有属性/属性的数组

    Array
(
    [0] = WP_Post Object
        (
            [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] = About
            [post_excerpt] = 
            [post_status] = publish
            [comment_status] = closed
            [ping_status] = closed
            [post_password] = 
            [post_name] = about
            [to_ping] = 
            [pinged] = 
            [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
            [menu_order] = 1
            [post_type] = nav_menu_item
            [post_mime_type] = 
            [comment_count] = 0
            [filter] = raw
            [db_id] = 20
            [menu_item_parent] = 0
            [object_id] = 20
            [object] = custom
            [type] = custom
            [type_label] = Custom Link
            [title] = About
            [url] = #
            [target] = 
            [attr_title] = 
            [description] = 
            [classes] = Array
                (
                    [0] = 
                )

            [xfn] = 
        )

    [1] = WP_Post Object
        (
            [ID] = 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] = Things I Can Do
            [post_excerpt] = 
            [post_status] = publish
            [comment_status] = closed
            [ping_status] = closed
            [post_password] = 
            [post_name] = things-i-can-do
            [to_ping] = 
            [pinged] = 
            [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
            [menu_order] = 2
            [post_type] = nav_menu_item
            [post_mime_type] = 
            [comment_count] = 0
            [filter] = raw
            [db_id] = 21
            [menu_item_parent] = 0
            [object_id] = 21
            [object] = custom
            [type] = custom
            [type_label] = Custom Link
            [title] = Things I Can Do
            [url] = #
            [target] = 
            [attr_title] = 
            [description] = 
            [classes] = Array
                (
                    [0] = 
                )

            [xfn] = 
        )

    [2] = WP_Post Object
        (
            [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] = A Few Accomplishments
            [post_excerpt] = 
            [post_status] = publish
            [comment_status] = closed
            [ping_status] = closed
            [post_password] = 
            [post_name] = a-few-accomplishments
            [to_ping] = 
            [pinged] = 
            [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
            [menu_order] = 3
            [post_type] = nav_menu_item
            [post_mime_type] = 
            [comment_count] = 0
            [filter] = raw
            [db_id] = 22
            [menu_item_parent] = 0
            [object_id] = 22
            [object] = custom
            [type] = custom
            [type_label] = Custom Link
            [title] = A Few Accomplishments
            [url] = #
            [target] = 
            [attr_title] = 
            [description] = 
            [classes] = Array
                (
                    [0] = 
                )

            [xfn] = 
        )

    [3] = WP_Post Object
        (
            [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] = Contact
            [post_excerpt] = 
            [post_status] = publish
            [comment_status] = closed
            [ping_status] = closed
            [post_password] = 
            [post_name] = contact
            [to_ping] = 
            [pinged] = 
            [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
            [menu_order] = 4
            [post_type] = nav_menu_item
            [post_mime_type] = 
            [comment_count] = 0
            [filter] = raw
            [db_id] = 23
            [menu_item_parent] = 0
            [object_id] = 23
            [object] = custom
            [type] = custom
            [type_label] = Custom Link
            [title] = Contact
            [url] = #
            [target] = 
            [attr_title] = 
            [description] = 
            [classes] = Array
                (
                    [0] = 
                )

            [xfn] = 
        )

)

我该怎么做?

我目前正在努力处理这些代码行

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-内容\主题\mySite\header.php:98

共有2个答案

舒嘉德
2023-03-14

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

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;       
}
澹台奇略
2023-03-14

我要把这个答案贴在这里供参考...

使用Foreach循环

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

因为要访问对象内部的数据,请使用-

例如:$data['post_date']$data-

 类似资料:
  • 无法理解为什么在尝试实现此角度代码时会出现TypeError。它在类({constructor:function(){}})周围生成错误。不确定原因。谢谢你的帮助

  • 问题内容: 我收到错误消息: 第183行上的“致命错误:无法将stdClass类型的对象用作数组” 从此代码: 有人知道上面的代码有什么问题吗?还是这个错误是什么意思? 问题答案: CodeIgniter将结果行作为对象而不是数组返回。从用户指南中: 结果() 此函数以 对象 数组或失败时 为空数组的形式 返回查询结果。 您必须使用以下符号访问字段:

  • 我对Vue相当陌生,我一直在尝试如何使用Axios调用Java API。 vue.config.js dashboard.vue 然而,无论我如何尝试,它总是给出错误: 在这个错误中,我编辑了dashboard.vue:19的路径,但它来自那里。这行给出了错误:。 API运行在localhost端口8080上,Vue应用程序运行在localhost端口3000上。我不确定是我做错了跨原点还是完全不

  • 我正在修补,试图实现简单的路由。我按照他们的示例中所写的方式键入代码(但不包括s)https://github.com/rackt/react-router#whats-看起来像。 我在浏览器中得到这个错误: 未捕获错误:不变违反:元素类型无效:预期字符串(用于内置组件)或类/函数(用于复合组件),但得到了:对象。 这就是我要做的。 我在页面上附加脚本,和我的代码在。还有和和jQuery(所有三个

  • 我的职能是: 我在这条线上出错了。 我们需要解决它。请帮忙做。

  • 这是控制台告诉我的: TypeError:无法将未定义的转换为对象 (如果我把它从代码中删除,他对vertikal也是这么说的) 除了其他一些空字符串,这是我代码的开始。。。错在哪里?是不是太多了以至于我看不见?