0

I have a custom post type (gallery). On the archive page, I'm limiting the display to 12 items, with pagination links below. This works as it should, but I'm getting error notices:

Notice: Undefined variable: query in /var/www/wp-content/themes/sal-custom_strap/archive-gallery.php on line 78

Notice: Trying to get property 'max_num_pages' of non-object in /var/www/wp-content/themes/sal-custom_strap/archive-gallery.php on line 78

Here is the code (line 78 is the second to last line) :

 <div class="card-deck justify-content-center" id="gallery-results">    

                        <?php
                        while ( have_posts() ) :
                            the_post();
                
                            get_template_part( 'template-parts/content', 'gallery' );
                
                        endwhile; // End of the loop.
                        ?>
    
    </div><!-- .card-deck -->
    <div class="nav-center">
        <div class="nav-next"><?php previous_posts_link( '<–– Previous' ); ?></div>
        <div class="nav-previous"><?php next_posts_link( 'Next ––>', $query->max_num_pages ); ?></div>
    </div>

I wrote this code a while ago and haven't been using php since, so I suspect I am forgetting something basic. What am I missing?

thanks.

1 Answer 1

0

You're using $query-> but there is no variable named $query, it's just pulled out of thin air, it has to come from somewhere, so PHP is generating notices complaining about it. It would be like if I answered the question with "you need to apply gabobobobble" or some other made up word.

So instead, look up next_posts_link:

https://developer.wordpress.org/reference/functions/next_posts_link/

The official docs say the second parameter is:

$max_page

  • (int) (Optional) Max pages. Default 0.

Since it's optional you can remove it.

3
  • thankfully gabobobobbble isn't a thing or that could have ended badly
    – Tom J Nowell
    Commented Jul 6, 2022 at 13:47
  • I can't stop laughing at "gabobobble." When I'm done laughing I'll try to understand your suggestions, which I thank you for. Will update.
    – Amy
    Commented Jul 6, 2022 at 15:01
  • 1
    Ah, I got it now, removed the offending code so line 78 is now "<div class="nav-previous"><?php next_posts_link( 'Next ––>' ); ?></div>" and all works as it should. Thanks again!
    – Amy
    Commented Jul 6, 2022 at 16:05

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.