Aller au contenu
FR

Display a Custom Post Type’s Categories

Version audio de cet article

Simply create a new Post Type and a taxonomy for your new Custom Post Type.

Once the taxonomy is set and your post type posts are published, here’s the query to use (in our example, we want the Region taxonomy):

This query can be used with the archive-region.php or taxonomy.php templates

Display a Custom Post Type's Categories

<?php 
$taxonomy = 'region'; // get the term IDs assigned to post. 
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 
	'title_li' => '',
	'style'    => 'none',
	'echo'     => false,
	'taxonomy' => $taxonomy,
	'include'  => $term_ids
);
$terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );
// display post categories
echo  'Région : '  . $terms;
}
?>