Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

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

1 Answer 1

up vote 1 down vote accepted

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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