How to Override “Blog pages show at most” in tag.php

I’m in the middle of developing a site (http://he.leavingworkbehind.com/) which features a single post on the home page, as determined by the “Blog pages show at most” option in Settings > Reading.

However, I’ve created a tag.php page where I’d like a complete list of posts (for the relevant tag) to be displayed. I’ve managed to find the code that can create that list:

Read More
<?php

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

<ul class="post-tag-list">
        <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
</ul>

<?php endwhile; endif; ?>

The list is only returning one result though — presumably because of the aforementioned “Blog pages show at most” setting.

So my question is this: how do I override this setting? To be more specific, what exact code should I insert on tag.php?

I have a feeling that I may have already seen the answer here on Stack Exchange but am too ignorant of PHP coding to implement it successfully. Your help would be much appreciated!

Cheers,

Tom

Related posts

1 comment

  1. what exact code should I insert on tag.php?

    Nothing

    To alter the main query, use the pre_get_posts action, which runs before the main query and before the template is loaded. If you modify a query in the template, you’re running an additional query, which is a waste of resources.

    This would go in your theme’s functions.php, or a plugin.

    function wpa_tag_posts_per_page( $query ) {
        if( ! is_admin()
            && $query->is_tag()
            && $query->is_main_query() ) {
                $query->set( 'posts_per_page', 10 );
        }
    }
    add_action( 'pre_get_posts', 'wpa_tag_posts_per_page' );
    

Comments are closed.