When you need to create a category listing page, you need to create a page called category.php. This is the page you would go to when you click a link that lists all the categories of available posts.
But what do you do when you have a custom post type (CPT) and custom categories set up for your template in WordPress. Your average categories.php page won’t work in this scenario.
You will first need to set up your custom post type and the custom post type taxonomy in order to proceed any further with this tutorial.
The following are steps you need to follow when creating a CPT category page.
taxonomy-[custom-taxonomy-name].php
// Set up the arguments to pass through the query
$args = array(
'title_li' => __( '' ),
'type' => '[your_post_type]',
'child_of' => 0,
'parent' => '',
'orderby'=> 'name', // order by name
'order' => 'ASC', // order the name by asending order
'hide_empty' => 1, // hides all hidden
'hierarchical' => 1,
'taxonomy' => '[custom-taxonomy-name]',
'pad_counts' => false );
// Inititial the CPT query
$categories = get_categories($args);
// Display the categories in a list
echo '<ul class="categories-nav">';
wp_list_categories( $args );
echo '</ul>';
//Get the taxonomy / term query
$category = get_queried_object();
// Set up the arguments to pass through the query
$args = array(
'post_type' => '[your_post_type]',
'posts_per_page' => 9,
'post_status' => 'publish', // display only published posts
'tax_query' => array(
array(
'taxonomy' => '[custom-taxonomy-name]',
'field' => 'id', // sort by the Taxonomy ID
'terms' => $category->term_id, // display the ID of the current CPT category being selected
)),
);
// set up the wp_query
$wp_query = new WP_Query( $args );
// display the content
if ( $wp_query-> have_posts() ) {
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
the_title(); // the post title
the_content(); // the post content
}
}else{
echo "There are no BluePrints in this category; please try the subcategory on the left.";
}
Please let us know if you have any queries or questions, or if you had a different approach to creating custom post types category pages. If you do need WordPress support please reach out to our development team who will be more than happy to support you with your project.