12% off of LTD using this coupon: TWELVEPERCENTOFF. Promo ends on 2 Dec midnight UTC.
Published on Oct 22, 2021

Taxonomy Terms List in WordPress

Sridhar Katakam

Looking to show all the terms of a given taxonomy as an unordered list in WordPress?

The code snippet below will output a list of term names linking to their corresponding term archives and the number of posts in that term of the specified taxonomy.

<?php
$terms = get_terms( array(
	'taxonomy' => 'product_cat', // set your taxonomy here
	'hide_empty' => false, // default: true
) );

if ( empty( $terms ) || is_wp_error( $terms ) ) {
	return;
}

echo '<ul>';

foreach( $terms as $term ) {
	printf(
		'<li><a href="%s">%s</a> <span class="term-count">(%s)</span></li>',
		esc_url( get_term_link( $term ) ),
		esc_attr( $term->name ),
		$term->count
	);
}

echo '</ul>';

?>

Replace product_cat in the above with the name of your taxonomy.

Default ordering is by name in ascending order.

If you want to show the latest terms at the top i.e., order by ID in descending order, replace

$terms = get_terms( array(
	'taxonomy' => 'product_cat', // set your taxonomy here
	'hide_empty' => false, // default: true
) );

with

$terms = get_terms( array(
	'taxonomy' => 'product_cat',
	'orderby' => 'ID', // default: 'orderby' => 'name',
	'order' => 'DESC',
	'hide_empty' => false, // default: true
) );

Reference

tagschevron-leftchevron-rightchainangle-rightangle-upangle-downfolder-omagnifiercrossmenuchevron-downarrow-right