• joelgilmore

    (@joelgilmore)


    Hi all,

    I’ve been playing around with category icons, etc, and just wanted to check the use of get_the_category(). In the Codex Wiki, it says it returns an array of the categories associated with a given post_id. It also says that to retrieve the first category associated with a post, I might do

    $cats = get_the_categories();
    $first_cat= $cats[0];

    However, upon closer examination of both the source (function update_category_cache() in functions.php) and experiments, it seems that the array returned is indexed BY category ID. E.g., if categories 2 and 5 are set, then $cats[0] would be NULL, while $cats[2]->cat_ID would be 2.

    Can someone confirm that this is the true behaviour? Or am I missing something? (Just wanted to make sure before I update the Wiki…) It seems like an odd way of doing it; more often I would think I would want to retrieve each category associated with a post, one after the other (which, of course, I could do with a foreach statement, but…) This method makes it easy for me to know whether a particular category is assigned to a given post, but what situations is that useful?

    FWIW, my workaround is simply:

    unset($post_cat);
    foreach ((get_the_category()) as $cat){
    $post_cat[] = $cat;
    }

    which returns an integer indexed array of categories.

    Cheers,
    Joel

Viewing 2 replies - 1 through 2 (of 2 total)
  • Kafkaesqui

    (@kafkaesqui)

    It looks like you’re confusing things here; update_category_cache() and get_the_category() are not related functions. The former deals with an array of all categories on a blog, and in that case the array key (for $cache_categories) will be equal to the category ID. However the latter will only be those categories associated with a post, and the key will not be tied in any way to category (except by order they’re added to the array).

    Thread Starter joelgilmore

    (@joelgilmore)

    Hi Kafkaesqui,

    Thanks for your response. (I did realise that update_category_cache is to store a list of all the categories, but it’s accessed from within get_the_category via update_post_category_cache.)

    Reading the code again now, I realise that get_the_category() calls sort() on the array, which should reduce it to being indexed by 0,1,2 etc. and give the correct documented effect. Sorry for not being more thorough! However, if the array has only one element, it seems that sort (at least on my platform) doesn’t re-index the array. e.g., this
    <?php $test=array(5=>'beetle'); sort($test); echo '0:'.$test[0].'--- 5:'.$test[5].'---'; ?>
    returns
    0:--- 5:beetle---
    For posts with multiple categories, it works fine.

    I tried googling for this effect, and it seems that it was a bug at some time, but has perhaps been resolved. I guess I need to check what version of PhP my server runs. [Checked: It’s 4.2.2.]

    Can anyone else confirm or deny this effect?

    Cheers,
    Joel

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get_the_category() definition clarification’ is closed to new replies.