Title: boby's Replies | WordPress.org

---

# boby

  [  ](https://wordpress.org/support/users/boby/)

 *   [Profile](https://wordpress.org/support/users/boby/)
 *   [Topics Started](https://wordpress.org/support/users/boby/topics/)
 *   [Replies Created](https://wordpress.org/support/users/boby/replies/)
 *   [Reviews Written](https://wordpress.org/support/users/boby/reviews/)
 *   [Topics Replied To](https://wordpress.org/support/users/boby/replied-to/)
 *   [Engagements](https://wordpress.org/support/users/boby/engagements/)
 *   [Favorites](https://wordpress.org/support/users/boby/favorites/)

 Search replies:

## Forum Replies Created

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

 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [Testing and obtaining category_parent ID](https://wordpress.org/support/topic/testing-and-obtaining-category_parent-id/)
 *  [boby](https://wordpress.org/support/users/boby/)
 * (@boby)
 * [20 years, 6 months ago](https://wordpress.org/support/topic/testing-and-obtaining-category_parent-id/#post-62984)
 * Hello!
 * I was searching for something like this too and I wrote my own function because
   I didn’t found anything useful 🙁 or didn’t knew how to use the “get_the_category_parents()”
   function…
 * So here it is:
    `<?php function get_parent_category($id) { global $wpdb;
 *  $category = $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename, 
   category_description, category_parent FROM ".$wpdb->categories." WHERE cat_ID
   =".$id);
 *  if( isset($category) && !empty($category) ) :
    $category_output = array(); $
   category_output['cat_ID'] = $category[0]->cat_ID; $category_output['cat_name']
   = $category[0]->cat_name; $category_output['category_nicename'] = $category[0]-
   >category_nicename; $category_output['category_description'] = $category[0]->
   category_description; $category_output['category_parent'] = $category[0]->category_parent;
   return $category_output; else : return FALSE; endif; } ?>
 * You will use it like this for example:
    `<?php if( $cat > 0 ) : $this_category
   = get_parent_category($cat); ?> <h2><?php echo $this_category['cat_name']; ?>
   </h2> <?php echo $this_category['category_description']; ?> <?php endif; ?>
 * Hope you’ll understand. You will get “cat_ID”, “cat_name”, “category_nicename”,“
   category_description” and “category_parent” in the variable that you assign the
   results of the “get_parent_category()” function.
    “get_parent_category($cat)”
   is the call of the function, where “$cat” is the current category_ID and is usually
   direct available.
 * Hope this works. Maybe you’ll have to change it a bit, this is what I needed 
   for my page.
 * Cheers, Boby!
 *   Forum: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
   
   In reply to: [Categories and Descriptions](https://wordpress.org/support/topic/categories-and-descriptions/)
 *  [boby](https://wordpress.org/support/users/boby/)
 * (@boby)
 * [20 years, 6 months ago](https://wordpress.org/support/topic/categories-and-descriptions/#post-277277)
 * Hello! I was just looking for something like this but I haven’t found, so I wrote
   a simple script. You have probably to change some things to fit your needs. This
   is just questions 1, for question 2 you must do that or wait for another post:]
 * `<?php
    $main_categories = $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename,
   category_description, category_parent FROM ".$wpdb->categories." WHERE category_parent
   =0 ORDER BY cat_name");
 *  foreach($main_categories as $main_cat)
    { ?> <h2 class="category_name">cat_ID;?
   >" title="Browse category <?php echo $main_cat->cat_name; ?>"><?php echo $main_cat-
   >cat_name; ?></h2> <p class="category_description"><?php echo $main_cat->category_description;?
   >
 *  <?php
    $sub_categories = $wpdb->get_results("SELECT cat_ID, cat_name FROM ".
   $wpdb->categories." WHERE category_parent=".$main_cat->cat_ID." ORDER BY cat_name");?
   > <?php if( isset($sub_categories) && !empty($sub_categories) ) { ?> <p class
   ="category_subcategories">Subcategories: <?php foreach( $sub_categories as $sub_cat){?
   >  cat_ID; ?>" title="Browse category <?php echo $sub_cat->cat_name; ?>"><?php
   echo $sub_cat->cat_name; ?> * <?php } } ?>
 *  <?php
    } ?>
 * This will display only subcategories if there are some available.
 * Eg:
    Category A Description of Category A Sub Categories: Sub Cat1 | Sub Cat2
   | Sub Cat3
 * Category B
    Description of Category B
 * Cheers, Boby

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