0

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?

2 Answers 2

2

You can create a taxonomy-people_categories.php or a more general taxonomy.php (which will display all your taxonomies in this category - excluding post categories) - please see Taxonomy Templates / Template Hierarchy.

As far as a what you need to display your posts, once you have the taxonomy-people_categories.php created you can just run the normal WordPress loop:

<?php if(have_posts()) : ?>
    <?php while(have_posts()) : ?>
        <?php the_title(); ?>
    <?php endwhile; ?>
<?php endif; ?>
1
  • that's what I just did, but I get an error page "page not found"... what could be a reason?
    – MaxNagler
    Commented Mar 7, 2014 at 14:38
2

re: error 'page not found' did you flush your rewrites?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.