Results 1 to 3 of 3

Thread: Get Parent Category from Child Category

  1. #1
    Andrea Barghigiani is offline Hello World
    Join Date
    Sep 2010
    Location
    Italy, Leghorn
    Posts
    16

    Default Get Parent Category from Child Category

    Hi guys,

    really how do you do this?!?

    Let me explain a bit better my issue here, on my WP installation I've got several categories (300+ categories inside of my database) and I was trying to give to each one of them a proper RSS Feed.
    Basically the structure of my categories is as follow:
    Code:
    parent category
        child category
        child category
             grandchild category
        child category
    parent category
        child category
            grandchild category
        child category
    
    ... and so on
    Now I've read on the help of FeedBurner, and it says that I shall create a new feed for each category. So I am sure now you can see my problem, I do noto want to create a single RSS Feed for each of my category, so I've created an RSS Feed for each parent category and now comes my problem.

    I would like to display a link to the parent category for each children and grandchild categories so to find out the ancestor of the whole category system and I came up with this solution:
    Code:
    function parentCat( $cat ){
              $result;
              if( $cat->parent == 0 ){
                $result = $cat;
              } else{
                $result = get_term_by('id', $cat->parent, 'ad_cat');
                parentCat( $result );
              } 
              return $result;
    }
    //Variable to get the term object of the category
    $term = get_term_by('id', 'category_id', 'taxonomy_name');
    
    $test = parentCat( $term );
    As you can see this is a simple function that's supposed to run until the value of the category parent is set to 0; in this case the current category is a parent category!

    But instead to run the function untill it finds out the straight parent of the category, that in some cases is not the category with a parent value set at 0...

    Does anyone have an idea of how to find the anchestor category???

    Thanks in advance for the help!!

  2. #2
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,801

  3. #3
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,997

    Default

    Quote Originally Posted by Ryan View Post
    get_category_parents() should do it I assume:
    http://codex.wordpress.org/Function_...tegory_parents
    You beat me to the punch!
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •