Output a Custom Taxonomy Loop in WordPress

Author: 

WP-beaches


Description: 

Here is a custom taxonomy loop code snippet, which allows you to output a set of custom taxonomy terms using the get_terms object and a foreach loop.

 

In the above, a custom taxonomy named custom_taxonomy_name is assigned to the variable $terms and then used in a foreach loop to output as a plain html unordered list. One of the parameters passed in is to hide any terms which have no linked posts with the hide_empty value, more parameters can be found at https://developer.wordpress.org/reference/classes/wp_term_query/__construct/




Kind:

PHP


PHP:

'custom_taxonomy_name', // Swap in your custom taxonomy name
'hide_empty' => true,
));

echo '';

// Loop through all terms with a foreach loop
foreach( $terms as $term ) {
// Use get_term_link to get terms permalink
// USe $term->name to return term name
echo ''. $term->name .'';
}

echo '';


URL: