0

I am using tag.php to display content when a user visits a tag page. Everything I need is working great, however it is only showing the most recent 6 posts that have the tag. I need it to list all posts with the tag.

When I change the "Blog pages show at most" under Reading to a large number, it breaks part of my site so I would like to do it in the tag.php code.

I know you can list all posts by using the 'posts_per_page' => -1 parameter, but am having trouble implementing that into my code.

This is in my tag.php file

<?php 

        if ( have_posts() ) : ?>

            <?php $term_id = get_queried_object_id(); ?>
            <div class="pantry-thumbnail"><?php the_term_thumbnail( $term_id, $size = 'post-thumbnail', $attr = '' ) ?></div>


            <?php
                // Show an optional term description.
                $term_description = term_description();
                if ( ! empty( $term_description ) ) :
                    printf( '<div class="taxonomy-description">%s</div>', $term_description );
                endif;
            ?>

            <strong>Recipes</strong>


            <?php
                // Start the Loop.
                while ( have_posts() ) : the_post();

            ?>

            <ul>
                <li>
                  <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </li>
            </ul>


            <?
                endwhile;

                else :
                    // If no content, include the "No posts found" template.
                    get_template_part( 'content', 'none' );

                endif;

                wp_reset_query();
            ?>

Thank you for any help!

2
  • 1
    You might want to try a custom WP_Query since the default loop is only going to pull in a max of "blog pages show at most".
    – WebElaine
    Feb 13, 2017 at 19:41
  • I have tried creating a custom query, and all I was able to accomplish was listing all posts, not posts related to the tag. Would you mind helping me with doing that specifically in this code?
    – Hope
    Feb 13, 2017 at 19:43

1 Answer 1

3

I was able to fix this issue by adding this to the functions.php file

function wpa69774_limit_tags( $query ) {
 if ( $query->is_tag() && $query->is_main_query() ) {
    $query->set( 'posts_per_page', -1 );
 }
}
add_action( 'pre_get_posts', 'wpa69774_limit_tags' );

after finally stumbling upon this question: Can't Get 'tag' Page To Display only Tagged Posts

Your Answer

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

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