Reputation: 1141
I'm building a Wordpress site, and I'm trying to figure out how to modify taxonomy.php
to display all of the posts in a custom category and custom taxonomy, but I'm stuck. Here's the situation:
I have a custom post type called 'work.' Within that custom post type, I have a custom taxonomy called 'project type' where I have created several categories called photography, video, print design, etc. I set up a template called taxonomy.php
which governs mydomain.com/work as well as mydomain.com/project-type/print-design/. The problem is that these pages only display the 5 most recent posts. I need them to display all the posts in the proper category (or in the case of mydomain.com/work, all of the posts in the custom post type regardless of category).
I found this page about query_posts which helped me realize that I needed to add <?php query_posts( array ( 'post_type' => 'work', 'posts_per_page' => -1 ) ); ?>
. Now the work page is loading correctly, but all of the category pages are loading all the work posts (because I've defined the post_type as work in that snippet I added).
How can I generalize the code so that it makes the work page display all of the work posts and the category pages display only the relevant category posts? Here's the code in full:
<?php
/**
* Project Type Taxonomy Archive
*/
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
?>
<h3 style="color:#EEEEEE"><?php echo apply_filters( 'the_title', $term->name ); ?> PROJECTS</h3>
<ul class="thumbnails">
<div id="work">
<?php if ( !empty( $term->description ) ): ?>
<div class="archive-description">
<?php echo esc_html($term->description); ?>
</div>
<?php endif; ?>
<?php query_posts( array ( 'post_type' => 'work', 'posts_per_page' => -1 ) ); ?>
<?php if ( have_posts() ): while ( have_posts() ): the_post(); ?>
<div class="post">
<li class="span4">
<div class="thumbnail">
<a href="<?php the_permalink() ?>"><div class="tint"><?php the_post_thumbnail( 'featured-thumb' ); ?></div></a>
<br />
<div class="thumbcaption">
<a href="<?php the_permalink() ?>"><h3><?php the_title() ?></h3></a>
<p> <?php the_excerpt(); ?></p>
<p><i><?php the_terms( $post->ID, 'work_project_type' , ' ' ); ?></i></p>
</div>
</div>
</li>
</div>
<?php endwhile; ?>
<div class="navigation clearfix">
<div class="alignleft"><?php next_posts_link('« Previous Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Next Entries »') ?></div>
</div>
<?php else: ?>
<h2 class="post-title">No Projects in <?php echo apply_filters( 'the_title', $term->name ); ?></h2>
<div class="content clearfix">
<div class="entry">
<p>It seems there aren't any projects in <strong><?php echo apply_filters( 'the_title', $term->name ); ?></strong> right now. Check back later, I try to add new projects regularly.</p>
</div>
</div>
<?php endif; ?>
</div>
</div>
Upvotes: 0
Views: 4916
Reputation: 241
One way to deal with this would be to not add a custom template.
When your go to a url relating to a taxonomy archive wordpress has already got all the posts out of the database. By creating a separate template although you have easier control over the query args like 'posts_per_page' it would be nicer to not have to do your own query.
Instead consider doing a pre_get_posts filter. Something like this.
// if on a certain taxonomy archive page, do not limit the posts
add_action('pre_get_posts', function($query){
if (isset($query->query_vars['name_of_your_taxonomy'])) {
add_filter('post_limits', function($limit){
/*
default will be something like this 'LIMIT 0, 10'
but we don't want a limit so return empty string
*/
return '';
});
}
});
Upvotes: 1