WordPress ではカテゴリを取得する場合記事がひとつも登録されていないカテゴリを取得してくれませんが、get_terms() を使っている場合は以下のように hide_empty を false にしてあげれば記事数が0でもカテゴリを取得してくれます。
1 2 | $categories = get_terms('category', Array('hide_empty' => false)); print_r($categories); |
取得した結果は以下のような感じです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | Array
(
[0] => stdClass Object
(
[term_id] => 7
[name] => サッカー
[slug] => anime
[term_group] => 0
[term_taxonomy_id] => 7
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 0
)
[1] => stdClass Object
(
[term_id] => 1
[name] => 未分類
[slug] => %e6%9c%aa%e5%88%86%e9%a1%9e
[term_group] => 0
[term_taxonomy_id] => 1
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 11
)
) |

コメント