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
[description] =>
[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?
I’m guessing you’re referring to the
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.
… will give you what you are looking for.
$categories[$i]->count
and$categories[$i]->category_count
are the number of posts in the category, not the number of categories there are.count( $categories )
will give you the number of categories there are. Each array element represents a category.