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 made a tag.php under my theme folder in order to style all of the tagged post pages from urls like 'www.mywebsite.com/tag/kittens'.

However, if I go to 'www.mywebsite.com/tag/kittens' it displays all of my posts, even the ones that are not tagged 'kittens' or at all. I used the the standard loop, and I can't find a way to alter it to grab the tag page name and only return those posts without making a separate 'tag-kittens.php' type page for every single tag.

Help?


<?php
    get_header();
    query_posts( 'posts_per_page=5' ); ?>

<?php the_tags( $before, $sep, $after ); ?>

 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

 <div class="post">

 <!-- Display the Title as a link to the Post's permalink. -->
 <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

 <!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
 <small><?php the_time('F jS, Y') ?></small>

  <div class="entry">
    <?php the_content(); ?>
  </div>

 </div> <!-- closes the first div box -->

 <?php endwhile; else: ?>
 <p>Sorry, no posts matched your criteria.</p>
 <?php endif;
    get_footer();
 ?>
share|improve this question
    
Hard to help without seeing the template. Please add the code needed to reproduce the behavior. And welcome to WordPress Stack Exchange! –  toscho Oct 18 '12 at 22:24
    
I am literally just using the standard loop. –  iob Oct 18 '12 at 22:45
    
No, you don’t. You are using query_posts(), overwriting the main query. Remove that, and your template will work. –  toscho Oct 18 '12 at 22:51

1 Answer 1

The problem is this line:

query_posts( 'posts_per_page=5' );

You're overwriting the query for this page with a query for the 5 latest posts, regardless of tag, category, etc.. If you remove that line, your template will work as expected.

If you want to limit tag pages to only 5 posts, use a pre_get_posts action:

function wpa69774_limit_tags( $query ) {
    if ( $query->is_tag() && $query->is_main_query() ) {
        $query->set( 'posts_per_page', 5 );
    }
}
add_action( 'pre_get_posts', 'wpa69774_limit_tags' );
share|improve this answer
    
Works! Thank you very much. –  iob Oct 18 '12 at 23:07

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.