Title: Get Sub-Category
Last modified: August 19, 2016

---

# Get Sub-Category

 *  [jppinto](https://wordpress.org/support/users/jppinto/)
 * (@jppinto)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/get-sub-category/)
 * I am trying to pull back a link and title of a post’s sub-category in a loop.
 * PARENTCAT1
    -SUBCAT1 -SUBCAT2
 * PARENTCAT2
    -SUBCAT4 -SUBCAT5
 * 1) I want to return a link and title. i.e.
    `<a href="http://site/cat/subcat4"
   >SUBCAT4</a>` 2) I would like to change the SUBCAT4 name on the link 3) I should
   be able to specify the PARENTCAT2 or 1
 * This was kind of working, except I have to enter the category. I want it to use
   the category that the post is in and be able to only specify the PARENTCAT. A
   post will only be in ONE sub-category of the PARENTCAT
 *     ```
       <?php
   
           // Get the ID of a given category
           $category_id = get_cat_ID( 'SUBCAT4' );
   
           // Get the URL of this category
           $category_link = get_category_link( $category_id );
       ?>
   
       <!-- Print a link to this category -->
       <a href="<?php echo $category_link; ?>" title="Category Name">SUBCAT4</a>
       ```
   
 * I tried this also but it just brings back the PARENTCAT2
 *     ```
       <?php
   
           // Get the category name
           $category_name = get_cat_name(5); 
   
           // Get the ID of a given category
           $category_id = get_cat_ID( $category_name );
   
           // Get the URL of this category
           $category_link = get_category_link( $category_id );
       ?>
   
       <!-- Print a link to this category -->
       <a href="<?php echo $category_link; ?>" title="Category Name">SUBCAT4</a>
       ```
   

Viewing 1 replies (of 1 total)

 *  Thread Starter [jppinto](https://wordpress.org/support/users/jppinto/)
 * (@jppinto)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/get-sub-category/#post-1877915)
 * s.o.s

Viewing 1 replies (of 1 total)

The topic ‘Get Sub-Category’ is closed to new replies.

## Tags

 * [headache](https://wordpress.org/support/topic-tag/headache/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [jppinto](https://wordpress.org/support/users/jppinto/)
 * Last activity: [15 years, 3 months ago](https://wordpress.org/support/topic/get-sub-category/#post-1877915)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
