Can’t Get ‘tag’ Page To Display only Tagged Posts

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

Leave a Comment