WordPress.org

Ready to get started?Download WordPress

Forums

get_the_category pulling wrong cat (3 posts)

  1. brockman.eric
    Member
    Posted 1 year ago #

    I have a series of posts that are categorized under category '2005' and sub-category '1:12'.

    I'm pulling the name of the parent category (2005) into the sidebar of each post using

    <? $category = get_the_category(); echo $category[0]->cat_name; ?>

    The problem is that it's showing the sub-category (1:12) instead of the category (2005).

    I'm also pulling the sub-category name in another part using
    <? $category = get_the_category(); echo $category[1]->cat_name; ?>

    And that's adding the parent instead of the sub.

    The problems seems to be that the sub-category is labeled using a numbers instead of letters. For instance if I change the sub cat name to 'one : twelve' it works properly.

    Anyone know how to fix this?
    Thanks much for any feedback!

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    get_the_category() is showing categories in alphabetical order - not by hierarchy; therefore showing the 'first' category can seem to be quite random.

    http://codex.wordpress.org/Function_Reference/get_the_category

    there is no general easy fix.

  3. brockman.eric
    Member
    Posted 1 year ago #

    hmmm, is there a way to call them by hierarchy, but only show the 'first' category, that you know of?

    cheers,

Topic Closed

This topic has been closed to new replies.

About this Topic