Hi @joorkataa
It is possible to display the subcategories of a parent category on the category archive page in WordPress. Here is one way you can do this:
1. Open the category.php file.
2. Find the code that displays the main category title. This might look something like this:
<?php single_cat_title(); ?>
3. Below this code, you can add the following to display the subcategories:
<?php
$current_category = get_category(get_query_var('cat'));
$subcategories = get_categories( array(
'parent' => $current_category->cat_ID,
'hide_empty' => false
));
if ( $subcategories ) :
?>
<ul>
<?php foreach ( $subcategories as $subcategory ) : ?>
<li>
<a href="<?php echo get_category_link( $subcategory->term_id ); ?>"><?php echo $subcategory->name; ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
This code will retrieve the subcategories of the current category and display them in a list.
⚠️ Note: Make sure to create a child theme and add this code to your child theme’s category.php
template file, so your changes won’t be overwritten when the parent theme is updated.
Well, the code doesn’t work as it should
It also shows me under categories in which there is nothing, and I want it to show under categories with only posts 🙂