My post has only one category. The following code:
$categories = get_the_category();
gives me this result:
Array
(
[0] => stdClass Object
(
[term_id] => 22
[name] => Style Guide
[slug] => style-guide
[term_group] => 0
[term_taxonomy_id] => 22
[taxonomy] => category
Why is get_the_category() saying that I have two categories? =>
[parent] => 0
[count] => 2
[object_id] => 391
[cat_ID] => 22
[category_count] => 2
[category_description] =>
[cat_name] => Style Guide
[category_nicename] => style-guide
[category_parent] => 0
)
)
Why is it double up?
2 Answers
I’m guessing you’re referring to the
[category_count] => 2
when you say “saying that I have two categories”?
If so, you should understand that category_count is not the number of cateogries that this post has, but rather it is ‘the number of uses of this category (also stored as ‘count’)’ – see get_the_category function reference. The fact that there is only one array element in the returned object indicates that there is only 1 category assigned to this post.
count($categories)
… will give you what you are looking for.