我正在尝试检索 wordpress 帖子中标签的 slug,现在可以使用
获取所有标签信息$tag = wp_get_post_tags($post->ID);
有关这方面的更多信息,请访问 Wordpress Docs
通过使用它你应该得到像这样返回的数据......
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
)
)
现在我想要的是第一项的 slug,应该如下所示
$tag[0]['slug']
但是这样做我收到了这个 php 错误:
Cannot use object of type stdClass as array
有人能告诉我我做错了什么吗?获取 slug 数据的最佳方式是什么
最佳答案
请注意,该数组包含 objects(stdClass
的实例),而不是其他数组。所以语法是:
$tag[0]->slug
关于php - 使用 Wordpress '不能使用 stdClass 类型的对象作为数组',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6171699/