Your cart is currently empty!
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”
-
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!
-
Grabber Pro
Original price was: $59.$39Current price is: $39. -
Custom WooCommerce Checkbox Ultimate
Original price was: $39.$19Current price is: $19. -
Android App for Your Website
Original price was: $49.$35Current price is: $35. -
Abnomize Pro
Original price was: $30.$24Current price is: $24. -
Medical Portfolio Pro
Original price was: $31.$24Current price is: $24.
Latest Posts
- How to Create a PHP Remote File Downloader with Live Progress Bar

- How to Connect AWS CloudFront URL with a Cloudflare Subdomain

- Android Developer Interview Questions Categorized by Topic

- When Data Must be Sanitized, Escaped, and Validated in WordPress

- Alternative to WordPress for High Traffic News Websites: Node.js & Other Scalable Solutions







Leave a Reply