0

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();
 ?>
3
  • Hard to help without seeing the template. Please add the code needed to reproduce the behavior. And welcome to WordPress Stack Exchange!
    – fuxia
    Commented Oct 18, 2012 at 22:24
  • I am literally just using the standard loop.
    – iob
    Commented Oct 18, 2012 at 22:45
  • No, you don’t. You are using query_posts(), overwriting the main query. Remove that, and your template will work.
    – fuxia
    Commented Oct 18, 2012 at 22:51

1 Answer 1

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' );
0

Your Answer

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

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