php - 使用 Wordpress '不能使用 stdClass 类型的对象作为数组'

标签 php arrays wordpress tags

我正在尝试检索 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/

相关文章:

php - 动态创建的按钮隐藏在php中

arrays - Rust中的意外行为 “for in n..m”

mysql - 无法将 Time 对象保存到 Ruby on Rails 中的 Time 列数组中

php - 显示数组撇号内的回显

css - WordPress 菜单 + Bootstrap + Glyphicon 代替文本

php - utf-8转iso-8859-1编码问题

java - 在最终的 Jar 中包含 php 文件,以便方法可以调用 php 文件

javascript - 也许 PHP 还记得颜色?

wordpress - 404 当 wp_enqueue_script 我的 JS 文件时

php - 在保持锁定的同时读取和写入文件