尝试在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个元素)
发布于 2017-03-27 15:24:06
我想这可能对你有帮助。行后的$postcatjson = json_encode( $postcat );
你只需加上下面的一行:
$decode = json_decode($postcatjson, true);
echo $decode[0]['cat_ID'];https://stackoverflow.com/questions/43041675
复制相似问题