首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Wordpress在UI中的JSON输出

使用Wordpress在UI中的JSON输出
EN

Stack Overflow用户
提问于 2017-03-27 08:42:34
回答 1查看 71关注 0票数 0

尝试在wordpress中获取与当前帖子类别相关的所有帖子。

代码语言:javascript
复制
global $post;
$postcat = get_the_category( $post->ID );
$postcatjson = json_encode( $postcat );

从上面的代码中,我得到了数组中的输出,下面,我提到了数组。

代码语言:javascript
复制
array(1) { [0]=> object(WP_Term)#882 (16) { ["term_id"]=> int(4) ["name"]=> string(6) "Second" ["slug"]=> string(6) "second" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(4) ["taxonomy"]=> string(8) "category" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(1) ["filter"]=> string(3) "raw" ["cat_ID"]=> int(4) ["category_count"]=> int(1) ["category_description"]=> string(0) "" ["cat_name"]=> string(6) "Second" ["category_nicename"]=> string(6) "second" ["category_parent"]=> int(0) } } 

在对上面的数组进行编码之后,使用

代码语言:javascript
复制
$postcatjson = json_encode( $postcat );

JSON输出如下:

代码语言:javascript
复制
[{"term_id":4,"name":"Second","slug":"second","term_group":0,"term_taxonomy_id":4,"taxonomy":"category","description":"","parent":0,"count":1,"filter":"raw","cat_ID":4,"category_count":1,"category_description":"","cat_name":"Second","category_nicename":"second","category_parent":0}]

我需要提取cat_ID (上面提到的JSON中的第11个元素)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-27 15:24:06

我想这可能对你有帮助。行后的$postcatjson = json_encode( $postcat );

你只需加上下面的一行:

代码语言:javascript
复制
$decode  = json_decode($postcatjson, true);
echo $decode[0]['cat_ID'];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43041675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档