I need to know how to display only the selected categories per post page..

Example. I am working on a website for movies. I have a category called Genre, then I have sub categories attached to that like comedy, horror etc...

Then I have a category called Availability and sub categories with like, USA, Mexico, England.. etc..

So I'll need to have it set like this per post page.

Genres Action/Adventure, Drama, New Releases

Availability Untied States, England, France

I have this code, but I can't figure out how to separate between genre and Availability

$categories = get_the_category();
$separator = ' ';
$output = '';
if ( ! empty( $categories ) ) {
    foreach( $categories as $category ) {
        $output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
    echo trim( $output, $separator );
} ?>

1 Answer 1


If what I understand from your question is correct. You have a post that categorized in multiple categories and subcategories. I have already made the same in a website. use these following code bellow so a sub categories will displayed after the parent category.

//first  part
$allcats = get_the_category();
$parent_id = $all_ids = array();

foreach ($allcats as $cats) {
    //get all category id first
    $all_ids[] = $cats->term_id;

    // get top parent category
    if ($cats->category_parent === 0 ) {
        $parent_id[] =  $cats->term_id;

//second part
$separator = ' &raquo; ';
$term_ids = implode( ',' , $all_ids );

$hiearchy_cat  = '';
foreach ($parent_id as $parents) {
    $parent_name    = get_cat_name($parents);
    $parent_link    = get_category_link($parents);
    $parent_cat     = '<span class="parent"><a href="'.$parent_link.'">'.$parent_name.'</a></span>';

    //get all sub category with certain ids
    $child_cats = wp_list_categories(
        'child_of' =>  $parents, 
        'title_li' => '',
        'style'    => 'none',
        'echo'     => false,
        'taxonomy' => 'category',
        'include'  => $term_ids,

    $child_cats = rtrim( trim( str_replace( '<br />',  $separator, $child_cats ) ), $separator );
    $hiearchy_cat .= '<b>'.$parent_cat.'</b> &raquo; '.$child_cats.'<br>';

//last output   
echo $hiearchy_cat;

Hope it helps

  • I'm not too good with php. With your code, could you alter it to display the Childs of "Genre" titled Genre and the childs of "Territories" titles Availability. Is this possible? May 29, 2017 at 0:58
  • just try put it in your single.php and it output will be like your question. please let me know if any errors
    – rheeantz
    May 29, 2017 at 1:11
  • added to single.php added the <?php ?> symbols and nothing happens.. My post pages do not display the sub categories. I'm trying to make it like this example here. summerhillfilms.com/bills-diary If you scroll below the movie synopsis you'll see two sub categories. Genres and Territories. Territories is named Availabilities here. Also I am using SiteOrigin Page Builder on every post page, not sure if that matters. May 29, 2017 at 5:16
  • it's a bit weird if that shown nothing. What about replace my code with your existing code in the same place ?
    – rheeantz
    May 29, 2017 at 5:22
  • I got it working with a different code. Thanks for your help. May 29, 2017 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.