How do I display the posts from a custom post type on a category.php page?

I have set up a category.php according to this wordpress hierarchy diagram.

When I am on the page http://example.com/category/my-category I want to display all posts that are under my-category using the loop:

Read More
<?php if (have_posts()): while (have_posts()): the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php endwhile; endif; ?>

WordPress is not showing the posts.
I will also add that this is a custom post type, but that shouldn’t matter or should it?

I can use get_posts() but then what is the point of having a category.php?

How do I show the posts in the current category using the_loop?

Related posts

Leave a Reply

1 comment

  1. I will also add that this is a custom post type, but that shouldn’t matter.

    In fact, this is your problem.

    By default, the Category Archive Index will only query posts from the post post-type. You need to tell WordPress to do otherwise, by adding your custom post type to the query via pre_get_posts:

    function wpse140419_add_cpt_to_category_archive( $query ) {
        // Only modify the main query
        // on the category archive index page
        if ( $query->is_category() && $query->is_main_query() ) {
            // Add CPT to the query
            $query->set( 'post_type', array( 'post', 'your-cpt-slug' ) );
        }
    }
    add_action( 'pre_get_posts', 'wpse140419_add_cpt_to_category_archive' );