Custom wordpress loop pagination not working

I have a website and i have a few loops from different categories on one page. the posts display like i want them to. At the bottom of the page i want to display all of the posts from the site. i have also got this working correctly. however the posts wont paginate, when i click on “newer posts” i am taken from this url: sitename/blog/ to sitename/blog/page/2/ but there is nothing found there and im getting an error

“Oops! That page can’t be found . which is found in my 404 page.”

Read More

for my first two loops i have used the following code (with different queries)

<?php query_posts('cat=2&showposts=3'); ?>

however from research i have found this is the wrong way to do it and i should be querying posts like:

$the_query = new WP_Query($query_args);

So i have changed the query at the bottom of the page to this format, and temporarily removed the top two (wrong) queries. However im still getting the same error.

Could someone please help. My php knowledge is limited and this is stretching me. the full code i am using to display the post and paginate is:

<?php 

            $query_args = array(
                'cat' => '3',
                'post_type' => 'post',
                'posts_per_page' => 6,
                'paged' => $paged
            );

            // Get current page and append to custom query parameters array
            $query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

            $the_query = new WP_Query($query_args);

            // Pagination fix
            $temp_query = $wp_query;
            $wp_query   = NULL;
            $wp_query   = $the_query;


        ?>

            <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

        <div class="articleThirdBlock">

                <div class="post__thumbnail post__thumbnail--marginTop">
                    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(400,250)); ?></a>
                </div><!-- /.post__thumbnail -->

                <a href="<?php the_permalink();?>" class="other__link"><h2 class="other__title"><?php the_title(); ?></h2></a>


        </div><!-- /.articleThirdBlock -->  

            <?php endwhile; endif; ?>

            <?php wp_reset_postdata(); 

            // Custom query loop pagination
            previous_posts_link( 'Older Posts' );
            next_posts_link( 'Newer Posts', $custom_query->max_num_pages );

            // Reset main query object
            $wp_query = NULL;
            $wp_query = $temp_query;


            ?>

Related posts

1 comment

  1. Custom Pagination Like « prev 1 2 3 next »

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    $args = array(
        'post_type'=>'post', // Your post type name
        'posts_per_page' => 6,
        'paged' => $paged,
    );
    
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
        ?>
            <div class="articleThirdBlock">
                <div class="post__thumbnail post__thumbnail--marginTop">
                    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(400,250)); ?></a>
                </div><!-- /.post__thumbnail -->
                <a href="<?php the_permalink();?>" class="other__link"><h2 class="other__title"><?php the_title(); ?></h2></a>
            </div><!-- /.articleThirdBlock -->
        <?php
        endwhile;
    
        $total_pages = $loop->max_num_pages;
    
        if ($total_pages > 1){
    
            $current_page = max(1, get_query_var('paged'));
    
            echo paginate_links(array(
                'base' => get_pagenum_link(1) . '%_%',
                'format' => '/page/%#%',
                'current' => $current_page,
                'total' => $total_pages,
                'prev_text'    => __('« prev'),
                'next_text'    => __('next »'),
            ));
        }    
    }
    wp_reset_postdata();
    ?>
    

Comments are closed.