Pagination not working on Custom Page Template

Question

I searched a lot on this but I can’t figure out why my code is not working.

I published a page with slug “blog” that uses a custom built template “page-blog.php” that I build from editing the original “page.php”. I want to loop latest posts, with pagination.

For numbered pagination I tried several plugins. They all works on archive and category pages, but on page-blog.php I always get the same result. Any page I try to view gives me a 404 error. Only the loop on first page is working.

URLS are like this:

www.mywebsite.com/blog/ (page url, with first page loop, ok)

www.mywebsite.com/blog/page/2/ (404 error)

www.mywebsite.com/blog/page/3/ (404 error)

This is My page-blog.php full code.

    <?php get_header(); ?>

    <div id="primary" class="content-area col three-fourth two-columns-right">
        <main id="main" class="site-main" role="main">

            <?php while ( have_posts() ) : the_post(); // page loop ?>

            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <header class="entry-header">
                    <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
                    <?php edit_post_link( __( 'Edit', 'mywebsite' ), '<span class="edit-link">', '</span>' ); ?>
                </header>

                <div class="entry-content">
                    <?php the_content(); ?>
                </div>
            </article>

            <?php endwhile; // end of the page loop. ?>
            <?php wp_reset_postdata(); ?>

            <?php 
            // Begin secondary loop (pagination not working)
            $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;

            $args = array (
                'post_type'              => 'post',
                'post_status'            => 'publish',
                'paged' => $paged,
            );

            // The Query
            $news_query = new WP_Query( $args );

            // Preserving main query
            $temp_query = $wp_query;
            $wp_query   = NULL;
            $wp_query   = $news_query;

            ?>
            <?php if ( $news_query->have_posts() ) : ?>
                <?php while ( $news_query->have_posts() ) : $news_query->the_post(); ?>
                    <?php get_template_part( 'content', get_post_format() );?>
                <?php endwhile; ?>

                <?php  if(function_exists('wp_paginate')) {
                    wp_paginate();
                } ?>

                <?php wp_reset_postdata(); ?>

            <?php else : ?>

                <?php get_template_part( 'content', 'none' ); ?>

            <?php endif; ?>
            <?php 
            // Reset main query object
            $wp_query = NULL;
            $wp_query = $temp_query;    
            ?>

        </main><!-- #main -->
    </div><!-- #primary -->

<?php get_sidebar('blog'); ?>
<?php get_footer(); ?>
0
, , , , bluantinoo 8 months 0 Answers 62 views 0

Leave an answer