List categories, subcategories in Archive Page

List categories, subcategories in Archive Page

Enhance your WordPress site with this tutorial on displaying subcategories in archive.php. This step-by-step guide provides PHP code to retrieve and list subcategories based on the queried term, complete with links for easy navigation. Perfect for developers looking to improve site structure and user experience!

Use the following PHP code in archive.php

<?php

$term = get_queried_object();
$term_id = $term->term_id;
$taxonomy_name = $term->taxonomy;

$termchildren = get_term_children( $term_id, $taxonomy_name );

echo ‘<ul class=”sub-category-list”>’;
foreach ( $termchildren as $child ) {
$term = get_term_by( ‘id’, $child, $taxonomy_name );
echo ‘<li><a href=”‘ . get_term_link( $term, $taxonomy_name ) . ‘”>’ . $term->name . ‘</a></li>’;
}
echo ‘</ul>’;

?>

Al Mahbub Khan
Written by Al Mahbub Khan Full-Stack Developer & Adobe Certified Magento Developer

Full-stack developer at Scylla Technologies (USA), working remotely from Bangladesh. Adobe Certified Magento Developer.