Get Sub category ID’s of a post

You can use this code to get the subcategory ID’s of a post.

<?php
     $categories = get_the_category();
     $this_cat_ID = $categories[0]->cat_ID;
     $this_cat_name = $categories[0]->cat_name;
     $this_cat_url = get_category_link($this_cat_ID);
     // get the sub category if we have them
     foreach ($categories as $cat) {
        $parent = $cat->category_parent;
        if ($parent != 0 ){
           $sub_cat_ID = $cat->cat_ID;
           $sub_cat_name = $cat->cat_name;
           $sub_cat_url = get_category_link($sub_cat_ID);
        }
     }
    ?>

 

You can echo out whatever you need.

Leave a Reply