I created a CPT which allows the user to add people to the page. I have already created a page template for displaying all people. This page also displays all categories used for people. My problem: How do I achieve that when the user clicks on a category only people who have this category are displayed?
Here are the relevant code excerpts:
function.php (registering the taxonomy):
function create_people_taxonomies() {
$labels = array(
'name' => _x( 'Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => __( 'Parent Category' ),
'parent_item_colon' => __( 'Parent Category:' ),
'edit_item' => __( 'Edit Category' ),
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New Category Name' ),
'menu_name' => __( 'Categories' ),
);
$args = array(
'hierarchical' => true, // Set this to 'false' for non-hierarchical taxonomy (like tags)
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'categories' ),
);
register_taxonomy( 'people_categories', array( 'people' ), $args );
}
add_action( 'init', 'create_people_taxonomies', 0 );
page-cpt-people.php (page template on which the categories are being displayed):
<?php
$taxonomy = 'people_categories';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></li>
<?php } ?>
</ul>
<?php endif;?>
?>
My questions:
- Which file name do I need to give to my taxonomy page so that the user is directed to this page when he clicks on one of the categories in page-cpt-people.php?
- Which code does the taxonomy page need to contain so that the loop displays only those people who have the category selected?