custom taxonomies on pages
-
16-10-2019 - |
Question
i need some help how can i display custom taxonomies on selected pages? i mean i have 3 custom taxonomies location,duration & courses i have created three pages where i want to display the terms of these custom taxonomies, i have displayed it on my posts by using function
<?php echo do_shortcode("[terms]"); ?>
but same thing i want to make display on my pages also, how can i do that??????
Solution
If you want to display a list of all terms in a taxonomy, you can call wp_list_categories()
and pass the taxonomy
argument to get anything other than the categories:
wp_list_categories( array(
'taxonomy' => 'your-taxonomy'
) );
If you want to use a shortcode for this, so you can use it in your post content, use add_shortcode()
. This untested example code allows you to use [taxonomy_terms]
or [taxonomy_terms taxonomy=my-taxonomy]
in your content:
add_shortcode( 'taxonomy_terms', 'wpse4668_taxonomies' );
function wpse4668_taxonomies( $atts )
{
// Sanitize our input
$atts = shortcode_atts( array(
'taxonomy' => 'your-default-taxonomy',
), $atts );
// Don't echo the output, just return it
$atts['echo'] = 0;
return wp_list_categories( $atts );
}