Uncaught Error: Cannot use object of type WP_Post as array
我试图将WordPress菜单放在模板文件中。 我发现使用wp_nav_menu很不舒服,因为它输出的标记与我的静态HTML模板不兼容。 我知道我可以使用自定义菜单浏览器类来修改wp_nav_menu的标记。 但是,我只想使用菜单名称及其相应的url并将其放置在我的模板中。 为此,我使用此函数获取菜单的所有属性/属性的数组。 这是此函数生成的代码...
我通过使用var_dump()找到了它。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | 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] = ) ) |
我的意图是获取" post_title"和" url"的值
我怎样才能做到这一点?
我目前正在努力处理这些代码行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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 url; ?>" class="active"><?php $menu_object->post_title; ?> </li> <?php endforeach; ?> <?php endif; ?> </ul> </nav> |
这将产生此错误:
Fatal error: Uncaught Error: Cannot use object of type WP_Post as array in D:\xampp\htdocs\mySite\wp-content\themes\mySite\header.php:98
我将在此处发布此答案以供参考...
使用
1 2 3 | foreach ($originalArray as $key => $data) { $post_date = $data->post_date; // 2017-07-10 11:50:32 } |
由于要访问对象内部的数据,请使用
例如:
您正在检索对象,可以将其转换为数组,如下所示:
1 2 3 4 5 6 7 8 9 10 11 |