Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.
To keep the posts in your existing loop but print only their child-category links, get each post’s categories, filter out top-level terms whose parent is 0, and render the remaining terms. This filters displayed links; it does not change which posts WordPress queried.
Filter category links inside the loop
The following template code shows direct child categories assigned to each post. A direct child is a category whose parent value points to another category.
<?php
while ( have_posts() ) :
the_post();
$categories = get_the_category();
$child_categories = array_filter(
$categories,
static function ( $category ) {
return 0 !== (int) $category->parent;
}
);
if ( $child_categories ) {
echo '<ul class="post-child-categories">';
foreach ( $child_categories as $category ) {
printf(
'<li><a href="%1$s">%2$s</a></li>',
esc_url( get_category_link( $category->term_id ) ),
esc_html( $category->name )
);
}
echo '</ul>';
}
endwhile;
?>
get_the_category() returns the categories assigned to the current post. The filter keeps terms with a nonzero parent, and get_category_link() supplies each archive URL. The escaping functions protect the generated URL and label before they are printed.
If a post has no child categories, this example prints no list at all. Add a fallback message or class only if your design needs one.
#1 Best Overall
Direct children versus every descendant
The test above identifies categories that have any parent, so it includes a category one level below the top level and deeper descendants. It does not, by itself, restrict results to the immediate children of a particular parent.
Only direct children of one known parent
When you know the parent category ID, compare each assigned term’s parent directly with that ID:
Rank #2
<?php
$parent_id = 12; // Replace with the intended parent category ID.
$categories = get_the_category();
$child_categories = array_filter(
$categories,
static function ( $category ) use ( $parent_id ) {
return (int) $category->parent === $parent_id;
}
);
?>
A nonzero parent alone would also allow child terms from unrelated branches, so use this comparison when the output belongs to one named branch.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Include descendants below a named parent
If “child” means children, grandchildren, and deeper terms under one branch, establish that ancestry rather than relying on parent > 0. For term queries, WordPress distinguishes immediate children from all descendants: parent => $id asks for direct children, while child_of => $id scopes the result to descendants. See the get_categories() reference for the term-query arguments and hierarchy behavior.
When the requirement is to change which posts appear
Sometimes “display only child categories” actually means “show only posts filed under categories in a child branch.” That is a query problem, not a link-rendering problem. Build the loop with WP_Query and choose the category argument whose descendant behavior matches the requirement.
| Argument | Descendants included? | Use when |
|---|---|---|
cat |
Yes, descendants of the selected category are included. | You want posts in a category and its child categories. |
category_name |
Yes, descendants are included. | You are selecting by the category’s slug. |
category__in |
No; it does not automatically include descendant categories. | You need the explicitly listed category IDs only. |
tax_query with include_children |
Controlled by the include_children setting for hierarchical taxonomies. |
You need explicit taxonomy-query behavior. |
For example, this query includes descendants of category ID 12 through the documented cat argument:
<?php
$child_posts = new WP_Query(
array(
'cat' => 12,
)
);
if ( $child_posts->have_posts() ) {
while ( $child_posts->have_posts() ) {
$child_posts->the_post();
// Post markup here.
}
wp_reset_postdata();
}
?>
Check the WP_Query reference before changing arguments, especially if exact-category matching matters.
Recommended Free Tools
Get a standalone list of child categories
If you are not listing categories attached to each post, query the category terms directly. get_categories() returns category objects that you can filter and format yourself:
Best Value
<?php
$children = get_categories(
array(
'parent' => 12,
'hide_empty' => true,
)
);
?>
parent => 12requests immediate children of category 12.child_of => 12scopes the result to descendants below category 12.hide_empty => trueomits terms with no posts; set it tofalsewhen empty categories should be visible.
Use wp_list_categories() when its generated category-list markup is sufficient. Use get_categories() when the template needs custom filtering, classes, attributes, or markup. The underlying term-query options are documented in the WP_Term_Query reference.
Filter categories centrally with a hook
If several templates should receive a filtered category array, WordPress provides the get_the_categories filter. It passes the categories and the post ID, allowing you to remove top-level terms before template code renders them. Use this approach carefully: it changes the data returned to every caller that uses the filtered function, so local filtering in one loop is safer when the rule applies to only one location.
Choose the correct approach
- Posts stay the same; only labels or links change: call
get_the_category(), filter the returned terms, and render them. - Only one parent branch should appear: compare
parentwith that parent ID or establish full ancestry for deeper descendants. - The loop must return a different set of posts: use
WP_Queryand selectcat,category_name,category__in, or a taxonomy query according to descendant rules. - A separate category navigation list is needed: use
get_categories()for custom output orwp_list_categories()for generated markup.
Behavior can vary with the category hierarchy and the WordPress version used by the site, so verify the result against the actual parent IDs, assigned terms, and empty-category setting in your installation.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

