John Davidson

php - Wordpress Display Child Term of the Current Category Page?


Hi I am trying to display the child term of the current post category page.
However I can figure out what am missing here. Thanks

$current_tax = get_query_var( 'category' );
$current_term = get_queried_object()->term_id;

$child_terms = get_terms( array('category' => $current_tax,
'child_of' => $current_term,
'orderby' => 'name' ));
if ( ! empty( $child_terms ) && ! is_wp_error( $child_terms ) ) {

echo '<div class="post-index__cate">';
foreach( $child_terms as $child ) {

printf( '<a class="category-name" href="%s">', get_term_link( $child, $child->taxonomy ));

echo $child->name. '</a>';

echo '</div>';

My Answer:
You can use the following code to display the child terms of the current category page in WordPress:

$current_category = get_queried_object();
$child_terms = get_terms( array(
'taxonomy' => $current_category->taxonomy,
'parent' => $current_category->term_id
) );

if ( ! empty( $child_terms ) ) {
echo '';

This code first gets the current category page using `get_queried_object()` and then retrieves the child terms of that category using `get_terms()`. It then loops through the child terms and displays them as a list with links to their respective term pages.

You can place this code in your theme's template file where you want to display the child terms of the current category page.

Rate this post

3 of 5 based on 7029 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us