我正在嘗試閱讀每個類別下的帖子,如下所示。有多個類別,標識爲「4」,「3」,「2」,「1」。只有類別「1」纔有可用的帖子。PHP - Foreach變量僅在讀取時纔有效
foreach ($wp_categories as $wp_category) {
$id = $wp_category->term_taxonomy_id;
//if($id == "1") {
$posts = $json_api->introspector->get_posts(array(
'cat' => $id
));
//}
$result[] = $posts;
}
return $result;
在上面的代碼中,如果我只取消註釋if條件,我得到結果數組中的帖子。但如果這是評論我沒有得到結果的職位。
我是PHP新手,上面的條件沒有任何意義。如果有人能夠解釋爲什麼if條件使結果中的帖子可用,那將會非常有幫助。
傾銷下面
{
"status": "ok",
"0": {
"term_id": "5",
"name": "Category 5",
"slug": "category5",
"term_group": "0",
"term_order": "1",
"term_taxonomy_id": "5",
"taxonomy": "category",
"description": "",
"parent": "0",
"count": "0"
},
"1": {
"term_id": "2",
"name": "Category 4",
"slug": "category4",
"term_group": "0",
"term_order": "2",
"term_taxonomy_id": "2",
"taxonomy": "category",
"description": "",
"parent": "0",
"count": "0"
},
"2": {
"term_id": "3",
"name": "Category 3",
"slug": "category3",
"term_group": "0",
"term_order": "3",
"term_taxonomy_id": "3",
"taxonomy": "category",
"description": "",
"parent": "0",
"count": "0"
},
"3": {
"term_id": "4",
"name": "abc",
"slug": "abc",
"term_group": "0",
"term_order": "4",
"term_taxonomy_id": "4",
"taxonomy": "category",
"description": "",
"parent": "0",
"count": "0"
},
"4": {
"term_id": "1",
"name": "Others",
"slug": "others",
"term_group": "0",
"term_order": "5",
"term_taxonomy_id": "1",
"taxonomy": "category",
"description": "",
"parent": "0",
"count": "1"
}
}
您可以在這裏打印'$ wp_categories'數組。 – user2936213
你試過回顯$ id嗎? – rybo111
如果你只是留下評論,它不會顯示任何帖子? – Goikiu