How do I create a category.php template (e.g. category-testimonial.php) that ONLY displays
posts from a custom post type?
Here is my code to create the custom post type:
function testimonials_custom_init() {
$labels = array(
'name' => _x('Testimonials', 'post type general name'),
'singular_name' => _x('Testimonial', 'post type singular name'),
'add_new' => _x('Add New', 'testimonial'),
'add_new_item' => __('Add New Testimonial'),
'edit_item' => __('Edit Item'),
'new_item' => __('New Testimonial'),
'view_item' => __('View Testimonial'),
'search_items' => __('Search Testimonials'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => false,
'query_var' => true,
'rewrite' => array('slug','pages'),
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'has_archive' => 'testimonials',
'supports' => array('title','editor','thumbnail','excerpt',)
);
register_post_type( 'testimonials' , $args );
}
add_action( 'init', 'testimonials_custom_init' );
It sounds like you’re confusing taxonomies (tags and categories) and post types (post, page, custom post types). What you actually want to do is create an archive template for your custom post type like
archive-custom_post_type.php
.You’ll also need to make sure that when you create your custom post type with your call to
register_post_type()
that you sethas_archive => 'custom_post_type'
. Then when you navigate tohttp://yourdomain.com/custom_post_type
, you’ll go to your custom archive template.See WP’s documentation on register_post_type and Template Hierarchy for more info.
This question might be old, however, this could help other people who are trying to get a custom post type display a custom category template. Let’s say you have a custom post type called “events”. By default, you would want for example to have “category-events.php” and filter the categories only associated to this custom post type. WordPress won’t let you do that (yet maybe).
My workaround would be first to create a blank category.php in your child theme (or you will remove whatever is already in that child theme file and replace with the code below).
Then paste that code:
That’s it.
Now you will need to create your actual template file like “category-events.php”. Inside that file, you can copy/paste the code from your archive.php or any custom template you want with get_header(‘your-custom-header’); get_footer(‘your-custom-header’); and whatever content you need in between. of course, you want the code that lets you actually filter that category: