Get Categories and Subcategory For Specific Post

Categories:

Tutorial to call a parent category or subcategory for specific post using wp_list_categories in WordPress , also we show how to add category control to your customizer.

Display the categories (or terms from other taxonomies) assigned to a post ordered by parent-child category relationship. Similar to the function get_the_category_list() which orders the categories by name. This example must be used inside the loop. Read also Get Category in WP – Parent, Link, Subcategory, ID, Name, List

Get Categories and Subcategory For Specific Post

global $post;
$taxonomy = 'category';
 
// Get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
 
// Separator between links.
$separator = ', ';
 
if ( ! empty( $post_terms ) && ! is_wp_error( $post_terms ) ) {
 
 $term_ids = implode( ',' , $post_terms );
 
 $terms = wp_list_categories( array(
 'title_li' => '',
 'style' => 'none',
 'echo' => false,
 'taxonomy' => $taxonomy,
 'include' => $term_ids
 ) );
 
 $terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
 
 // Display post categories.
 echo $terms;
}

Get Subcategory For Specific Post

You need to assign ID of parent category into array where child_of is written change with your own ID

global $post;
$taxonomy = 'category';
// Get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// Separator between links.
$separator = ', ';
 if ( ! empty( $post_terms ) && ! is_wp_error( $post_terms ) ) {
 $term_ids = implode( ',' , $post_terms );
 $terms = wp_list_categories( array(
 'title_li' => '',
 'style' => 'none',
 'show_option_none' => '', 
 'child_of' => 2, //ID of category
 'echo' => false,
 'taxonomy' => $taxonomy,
 'include' => $term_ids
 ) );
 $terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
 // Display post categories.
 echo $terms;
 }

Get Subcategory For Specific Post using custom call at Theme option or Customizer

you need to add category drop down to your customizer so whenever user select a particular category to show child than it will be displayed.

Into Customizer

// create an empty array
$cats = array();
 
// we loop over the categories and set the names and
// labels we need
foreach ( get_categories() as $categoriesi => $categoryy ){
 $cats[$categoryy->term_id] = $categoryy->name;
}
 

$wp_customize->add_setting( 'CallHere', array(
 'default' => 1,
 'sanitize_callback' => 'absint'
) );
 
$wp_customize->add_control( 'cat_contlr_top', array(
 'settings' => 'CallHere',
 'label' => 'Top Category Select',
 'type' => 'select',
 'choices' => $cats,
 'section' => 'panel_tabs', // depending on where you want it to be
) );

Calling function

$CallHere = absint(get_theme_mod('MYCat'));

Code to show category

global $post;
$taxonomy = 'category';
// Get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// Separator between links.
$separator = ', ';
 if ( ! empty( $post_terms ) && ! is_wp_error( $post_terms ) ) {
 $term_ids = implode( ',' , $post_terms );
 $terms = wp_list_categories( array(
 'title_li' => '',
 'style' => 'none',
 'show_option_none' => '', 
 'child_of' => $CallHere,
 'echo' => false,
 'taxonomy' => $taxonomy,
 'include' => $term_ids
 ) );
 $terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
 // Display post categories.
 echo $terms;
 }

Comment you question and suggestion below also don’t forget to subscribe blog.

 

Comments

One response to “Get Categories and Subcategory For Specific Post”

  1. Caroline Avatar
    Caroline

    Thank you! I’ve been struggling and for months after this code “Get Subcategory For Specific Post”.
    I just wanted a code to echo the child term of a parent in my taxonomy. And finally, here it is! I’m so glad!

Leave a Reply

Your email address will not be published. Required fields are marked *