2

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:

<?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?

3
  • "I will also add that this is a custom post type, but that shouldn't matter." - are you sure about that? Apr 6, 2014 at 15:03
  • @ChipBennett Editted :) Apr 6, 2014 at 15:05
  • @ChipBennett Also changed question title to better match the problem. Apr 6, 2014 at 15:13

1 Answer 1

3

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' );
2
  • Thanks for the incredibly fast answer! Yes, that was the issue. Apr 6, 2014 at 15:11
  • No problem! :) If it solved your issue, please accept it (by clicking the checkmark). Apr 6, 2014 at 15:41

Your Answer

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

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