Title: Shortcode to show the (child)categories a post belongs to
Last modified: August 21, 2016

---

# Shortcode to show the (child)categories a post belongs to

 *  [ysan](https://wordpress.org/support/users/ysan/)
 * (@ysan)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/a-10/)
 * Hello,
 * I’m looking for a shortcode to add in a post in order to show the categories 
   a post belongs to.
 * An example. I have a category ‘region’ with some subcategories :
 * Region
    – region A – region B – region C
 * I would like to add a shortcode in my posts that shows the subcategories of ‘
   Region’.
 * Again an example :
 * This activity is possible in [shortcode-to-show-the-childcategories-of-region].
 * Hope to find some help here.
 * Thanks in advance!!
 * Y

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

 *  Thread Starter [ysan](https://wordpress.org/support/users/ysan/)
 * (@ysan)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/a-10/#post-3854462)
 * I think i have found the php code for showing the childcategories inside a post.
   This code works inside the loop.
 *     ```
       <?php
       $sep = '';
       foreach((get_the_category()) as $childcat) {
       if (cat_is_ancestor_of(27, $childcat)) {
       echo $sep.'',$childcat->cat_name,'';
       $sep = ' / ';
       }}
       ?>
       ```
   
 * How to turn it into a shortcode [region] that can be used inside a post?
 *     ```
       function region($atts) {
       $sep = '';
       foreach((get_the_category()) as $childcat) {
       if (cat_is_ancestor_of(27, $childcat)) {
       echo $sep.'',$childcat->cat_name,'';
       $sep = ' / ';
       }}}
   
       add_shortcode('region', 'region');
       ```
   
 * seems to work… but it places the childcategories above the post and not inside
   the text.
 * Almost there… just need that little bit of help 😉
 *  [Siobhan](https://wordpress.org/support/users/siobhyb/)
 * (@siobhyb)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/a-10/#post-3854593)
 * Hi ysan. Did you manage to find a solution to this problem?
 *  Thread Starter [ysan](https://wordpress.org/support/users/ysan/)
 * (@ysan)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/a-10/#post-3854594)
 * Hello siobhyb,
 * This code does integrate a list of child categories
 * In this case [region]
    Change 35 into the parent category Change
 *     ```
       $sep = ', ';
       ```
   
 * into something else in order to have a different separator. For instance
 *     ```
       $sep = ' / ';
       ```
   
 * Good luck with your project!
 *     ```
       /**
        * Shortcode function to show child categories
        * [region]
        */
   
       function region($atts) {
       	$sep = '';
       	$output = '';
       	foreach((get_the_category()) as $childcat) {
       		if (cat_is_ancestor_of(35, $childcat)) {
   
       $output .= $sep . $childcat->cat_name;
       $sep = ', ';
       			}
       		}
       	return $output;
       }
       add_shortcode('region', 'region');
       ```
   
 * All thanks for this code goes to [alchymyth](http://www.transformationpowertools.com/wordpress/)
   who was so kind to help me with this code.
 *  [Siobhan](https://wordpress.org/support/users/siobhyb/)
 * (@siobhyb)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/a-10/#post-3854597)
 * Thanks for sharing the solution. 🙂

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

The topic ‘Shortcode to show the (child)categories a post belongs to’ is closed 
to new replies.

## Tags

 * [gnome-opw](https://wordpress.org/support/topic-tag/gnome-opw/)

 * 4 replies
 * 2 participants
 * Last reply from: [Siobhan](https://wordpress.org/support/users/siobhyb/)
 * Last activity: [12 years, 11 months ago](https://wordpress.org/support/topic/a-10/#post-3854597)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
