Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
I only see one array element, what seems to be the issue? –  m0r7if3r Feb 27 '12 at 18:51
    
[count] => 2, [category_count] => 2 –  Steven Feb 27 '12 at 18:55
add comment

2 Answers

up vote 4 down vote accepted

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.

share|improve this answer
1  
I like the way you think :D (though category_count is an object property, not an array element :P) –  m0r7if3r Feb 27 '12 at 18:57
    
Ah, that makes is a bit more clear.Better naming wouldn't hurt, e.g. usages. Thanks :) –  Steven Feb 27 '12 at 19:06
add comment

$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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.