0

I'm trying to build a post gallery page with pagination. Posts and pagination are displayed fine but for some reason pagination won't show the clicked page and remain on page 1. This is my code:

<?php /* My Template: Gallery Page */ ?>

<?php get_header(); ?>

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

        <div id="gridcontainer">
        <?php
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        $args = array(
            'author'        => 1,
            'post_per_page' => 8,
            'paged'         => $paged,
        );
        $the_query = new WP_Query( $args );

        global $wp_query;
        $tmp_query = $wp_query;
        $wp_query = null;
        $wp_query = $the_query;

        if ($the_query->have_posts()) : 
            while ($the_query->have_posts()) : $the_query->the_post();
                ?>
                            <div class="griditem">
                                <div class="postimage">
                                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail('category-thumbnail'); ?></a>
                                </div>
                                <h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                            </div>
                <?php           
            endwhile;
            the_posts_pagination( array( 'mid_size'  => 2 ) );
            wp_reset_postdata();

        else :
            // no post found code 
        endif;

        $wp_query = null;
        $wp_query = $tmp_query; 
        ?>  
        </div>  
    </main>

</div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Also I was using query_posts before but apparently that isn't good anymore. Is the code I'm using now up to date and problem-free besides the malfunctioning pagination?

  • Are you sure page #2, #3, etc. actually exist? I.e. Are there more than 8 posts for the query? Why don't you just use paginate_links() which allows you to use the $the_query without having to modify the global $wp_query? – Sally CJ Apr 27 at 0:03
  • As Sally said, paginate_links() would be your savior. I brought a code from Erik Larsson, I use for custom queries like ns_pagination($the_query). – Mayeenul Islam Apr 27 at 4:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.