• josefdunne

    (@josefdunne)


    Hi,

    I have a post, this post is in 3 categories. The category hierarchy is like so:

    • Fashion
    • Men
    • Shoes

    Fashion being the “root/top parent” category for the post. I want to display a title at the top of the post which says “Fashion”.

    I have tried “get_the_category” but this brings back an array in alphabetical order, I need only the root category which in my case is “Fashion”. So this is no use. I have tried “single_cat_title” but this brings back nothing.

    Any ideas on how to get the top category displayed as a title?

    Thanks

    Josef

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter josefdunne

    (@josefdunne)

    Some more information:

    I am looking for something like this: “get_category_parents” which would only display the top/root category not list them all. Maybe “get_category_parent” notice no S! Which would also work on a post page, as this is the problem I am having because I can use the single_cat_title for category or index pages.

    Thanks

    Josef

    moshu

    (@moshu)

    Thread Starter josefdunne

    (@josefdunne)

    I tried this, however the array is sent back in alphabetical order, I require the top/root parent so choosing [0] gives back “Bags” (I know I used “Shoes” in my example above but I will have a 3rd level category called “Bags”) What you suggest works to some extent but is not robust enough to use, since numbers of categories will be added (by the content admins of the site) and I don’t know what they will be, so I need a solution that gives me back the top/root parent category for a given post no matter what they are named etc.

    For example, here is my actual category listing at the moment (it may expand):

    • Fashion
    • Men
    • Accessories
    • Shoes
    • Women
    • Accessories
    • Bags
    • Kids & Baby
    • Lingerie
    • Shoes
    • Gifts
    • Interiors
    • Tech
    • Travel

    I would need the solution to work outside “The Loop”.

    hi josefdunne, I know you were searching for the solution to this problem 2 months ago now, but if you’re still trying to find it, then I think I’ve got the answer:

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

    This returns the category ID of the parent. Hope this helps.

    Cheers

    Tommy

    I ran into the same issue and after digging around, I wasn’t able to find any good solutions. So, I wrote my own. Basically it’s a short function that you give the category ID to. It’ll then go all the way up the category chain and return to you the root parent category ID.

    If it’s of any help, you can get it at http://www.yash-patel.com/yfactor/2008/12/25/getting-the-top-level-parent

    I hope that helps!
    Yash.

    I also ran into this and finally wrote some code to grab either the top category slug or the top category name: http://www.sandboxdev.com/blog/518/get-top-parent-category-wordpress/

    Hellou there,
    I want to add the name of the top parent category as a class to every post! I have nested my categories more than one level.

    this version seems to be good, but it only func. on the category page.
    http://www.sandboxdev.com/blog/518/get-top-parent-category-wordpress/comment-page-1/#comment-11751
    I need a solution for the frontpage too, have you any idea?

    thx alot!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Display the Top Parent (Root) category of a post as a title?’ is closed to new replies.