Support » Fixing WordPress » “get_the_category” returns wrong parents?

  • It appears the “get_the_category” function returns completely wonky results..

    The parent/child relationships are completely wrong.. It returns the correct names, but other stuff isn’t correct..


Viewing 9 replies - 1 through 9 (of 9 total)
  • Anyone?

    Hi, I’m struggling with sorting out the order as well. My purpose is to put a list of the categories/subcategories that a post belongs to within the post so that it’s completely evident what subject matter the post touches on.

    For me it returns all subcategories first, then all parents, instead of parent/sub, parent/sub as one would expect, and as the_category tag does. I can’t use the_category tag because I need a clean list without links and such.

    I asked someone for help but their code is wrong- it returns the same parent/cat all the time. But I don’t know enough about PHP to fix it.

    Here it is, just in case you do 🙂

    function ordered_cats() {

    foreach((get_the_category()) as $cat)

    $parents_1 = get_category_parents($cat);

    $parents = substr($parents_1, 0, -1);

    $string = $parents.”, “.$parents;
    // don’t have a clue what this is doing, doesn’t make any // sense to me at all. It should pull the elements from the // array and sort them into a string by parent/cat? weird

    echo $string;

    Sorry I couldn’t help more!

    Try using get_the_category_list() instead. If you don’t want the links in it, run strip_tags() on it to remove all the HTML from it. Like so:

    echo strip_tags(get_the_category_list());

    get_the_category_list() takes the same parameters as the_category() does.

    Thanks Otto! It does give you the parent/subcat structure, but it also echoes the parents out afterwards (just like the_category), so you get parent1/subcat, parent2/subcat, parent1, parent2.

    The workaround is just not to check the post as belonging to the parents, so I guess I’ll have to do that.

    Strip_tags is awesome though, I can use that to clean up two other lists- thanks a bunch for that tip!

    Hmm ok, ran into another aspect of the same question. I have a function that displays a different document title depending on what page is being viewed (page, single and archive for the categories).

    Using the get_the_category_list tag and stripping the HTML let me customize the single page’s document title perfectly, because it apparently works with the post information – the document title shows all of the categories/subcats that the post belongs to.

    However, that won’t work for the category (archive) document title. Posts can have more than one category/subcategory, so it’s no good for just displaying the current category/subcategory as a navigation reference.

    after reading through the template tags and experimenting for hours, I can’t seem to find a WP tag to display both the parent and subcat in the document title for a category (archive) page. I’m looking to present something like “Articles in Parent1/Subcat”. Instead all of the category template tags I can find return just “Articles in Subcat”, which is confusing since I have more than one subcat with the same name.

    Does anyone know of an outside-the-loop tag that can show the parent and subcat of a current category in just plain text? I tried breadcrumbs plugins but they return full HTML with link code, and that doesn’t work for the document title – it needs to be clean text.

    Thanks for your past help, and any future too!

    Darn, can’t edit my previous post – sorry. I found that get_category_parents works if you put it directly on the page, but not if you put it in functions.php? I need to call it via the custom header function, so that’s my only remaining issue with this now. Can anyone tell me why this isn’t working?

    Header function snip:

    elseif (is_archive()) {
    print ‘ Articles in ‘;
    print page_cats();

    // get cats function

    function page_cats() {
    echo(get_category_parents($cat, FALSE, ‘ ‘));

    Thanks in advance for any help!

    That function won’t work because you did not define $cat. What is $cat equal to?

    Huh, that explains why I don’t program PHP 😀 $cat is equal to the current category. I get why it works on a page and not in a function now. On a page it defaults to the current category, but in a function you need to define it.

    Now to look up how to define it in PHP lol. Thanks for your insight!

    And fixed, thanks to the other post from Palamedes.

    Final code:

    function page_cats() {
    if (is_category() == TRUE) {
    $cat = get_query_var(‘cat’);
    echo(get_category_parents($cat, FALSE, ‘ ‘));

    Output –
    Parent Subcat
    in plain text for use in your browser document title 😀

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘“get_the_category” returns wrong parents?’ is closed to new replies.