'不能将 stdClass 类型的对象用作数组' 使用 Wordpress

#39;Cannot use object of type stdClass as array#39; using Wordpress(不能将 stdClass 类型的对象用作数组 使用 Wordpress)
本文介绍了'不能将 stdClass 类型的对象用作数组' 使用 Wordpress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 wordpress 帖子中检索标签的 slug,现在可以使用

I am trying to retrieve the slug for a tag inside a wordpress post, now its possible to get all tag info using

$tag = wp_get_post_tags($post->ID);

Wordpress 文档

通过使用它,您应该得到像这样返回的数据...

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
       )

)

现在我想要的是第一项的 slug 应该如下

Now what I want is the slug for the first item which should be as follows

$tag[0]['slug']

但是这样做我收到了这个 php 错误:

However by doing so I recieve this php error:

不能将 stdClass 类型的对象用作数组

Cannot use object of type stdClass as array

有人可以告诉我我在这里做错了什么吗?以及获取 slug 数据的最佳方法是什么

Can someone tell me what I'm doing wrong here? and whats the best way to get the slug data

推荐答案

请注意,该数组包含 objects(stdClass 的实例),而不是其他数组.所以语法是:

Note that the array contains objects (instances of stdClass), not other arrays. So the syntax is:

$tag[0]->slug

这篇关于'不能将 stdClass 类型的对象用作数组' 使用 Wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)