php 使用 Wordpress '不能使用 stdClass 类型的对象作为数组'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6171699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'Cannot use object of type stdClass as array' using Wordpress
提问by Ian
I am trying to retrieve the slug for a tag inside a wordpress post, now its possible to get all tag info using
我正在尝试检索 wordpress 帖子中标签的 slug,现在可以使用以下方法获取所有标签信息
$tag = wp_get_post_tags($post->ID);
More info on this on the Wordpress Docs
有关Wordpress Docs 的更多信息
By using this you should get data returned like this...
通过使用它,你应该得到像这样返回的数据......
Array
(
[0] => stdClass Object
(
[term_id] => 4
[name] => tag2
[slug] => tag2
[term_group] => 0
[term_taxonomy_id] => 4
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 7
)
[1] => stdClass Object
(
[term_id] => 7
[name] => tag5
[slug] => tag5
[term_group] => 0
[term_taxonomy_id] => 7
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 6
)
)
Now what I want is the slug for the first item which should be as follows
现在我想要的是第一个项目的 slug 应该如下
$tag[0]['slug']
However by doing so I recieve this php error:
但是这样做我收到了这个 php 错误:
Cannot use object of type stdClass as array
不能使用 stdClass 类型的对象作为数组
Can someone tell me what I'm doing wrong here? and whats the best way to get the slug data
有人可以告诉我我在这里做错了什么吗?以及获取 slug 数据的最佳方法是什么
回答by rid
Note that the array contains objects(instances of stdClass
), not other arrays. So the syntax is:
请注意,数组包含对象( 的实例stdClass
),而不是其他数组。所以语法是:
$tag[0]->slug
回答by itlunch
Another option should be to explicitly cast $tag[0] into an array:
另一种选择应该是将 $tag[0] 显式转换为数组:
$t = (array)$tag[0];
$t["slug"] = ...
Can't get it to work though
虽然无法让它工作