NB: Make sure you do a back up of your theme, files and database before attempting the tutorials
Last modified : Sep 04 2022
Estimated reading time : 1 minute, 42 seconds - 87 words
Share the post "List custom posts types taxonomy"
For this tutorial, it’s important that your custom post type is configured with register_taxonomy.
It’s a nice and easy way to list your custom post type’s categories. By default, we need to display post categories with wp_list_categories.
<?php wp_list_categories(); ?>
Here’s an example of register_taxonomy and the taxonomy that needs to be recuperated. In this case it’s collaborateurs_cat.
// Catégories custom post type
/* Voici donc la taxonomie */
register_taxonomy( 'collaborateurs_cat',
/* nom du custom post type */
array('collaborateurs'),
array(
'hierarchical' => true,
'labels' => array(
'name' => __( 'Catégories', 'nom du theme' ),
'singular_name' => __( 'Catégories', 'nom du theme' ),
'search_items' => __( 'Rechercher une catégorie', 'nom du theme' ),
'all_items' => __( 'Toutes les catégories', 'nom du theme' ),
'parent_item' => __( 'Catégorie parente', 'nom du theme' ),
'parent_item_colon' => __( 'Catégorie parente :', 'nom du theme' ),
'edit_item' => __( 'Éditer une catégorie', 'nom du theme' ),
'update_item' => __( 'Sauvegarder une catégorie', 'nom du theme' ),
'add_new_item' => __( 'Ajouter une catégorie', 'nom du theme' ),
'new_item_name' => __( 'Nouvelle catégorie', 'nom du theme' )
),
'show_admin_column' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'collaborateurs' ),
)
);You will need to customize it so it display the custom post type’s category taxonomy; the solution is to properly use its taxonomy. Here’s how to use get_terms.
<?php
// Récupérer la taxonomie dans une variable
$taxonomy = 'collaborateurs_cat';
// Variable avec le get_terms
$tax_terms = get_terms($taxonomy, array('hide_empty' => false));
?>