尝试在wordpress中获取与当前帖子类别相关的所有帖子。
global $post;
$postcat = get_the_category( $post->ID );
$postcatjson = json_encode( $postcat );
从上面的代码我得到数组输出,下面,我提到了数组。
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) } }
使用
对上述数组进行编码后$postcatjson = json_encode( $postcat );
JSON输出如下:
[{"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个元素)
答案 0 :(得分:0)
我认为它可能对你有所帮助。在行 $ postcatjson = json_encode($ postcat);
之后您只需添加以下行:
$decode = json_decode($postcatjson, true);
echo $decode[0]['cat_ID'];