Pagination not working on Custom Page Template

MOST IMPORTANT EDIT

When I tested your code, I always added the page header name, and never realized that this was one thing that you didn’t add. You should always name a custom page template so that wordpress knows it’s a page template. So you need to edit your custom page template and add the header as follows

<?php
/**
 * Template Name: Name of your template
 */
get_header(); ?>

ORIGINAL ANSWER

You have to take a look at the pagination parameters of WP_Query.

  • paged (int) – number of page. Show the posts that would normally show up just on page X when using the “Older Entries” link.

  • page (int) – number of page for a static front page. Show the posts that would normally show up just on page X of a Static Front Page.

If you set this page template as a home page, your code works and paginate as it should. However, if you use this page template as a post/blog page, you will need to change

$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;

to

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

One point of note, the plugin you are using (WP Paginate) hasn’t been updated in more that two years. Although it still works, I would recommend that you have a look at other plugins that are up to date to avoid issues later.

Alternatively, you can use this function for pagination. Who the original author is, I’m not sure, but whoever you are my friend, credit goes to you.

The following code goes into functions.php

if ( ! function_exists( 'pietergoosen_pagination' ) ) :

    function pietergoosen_pagination($pages="", $range = 2) {   
       $showitems = ($range * 2)+1;  

      global $paged;
      if(empty($paged)) $paged = 1;

          if($pages == '')
          {
             global $wp_query;
             $pages = $wp_query->max_num_pages;
                if(!$pages)
                {
                $pages = 1;
                }
            }   

           if(1 != $pages)
          {
            $string = _x( 'Page %1$s of %2$s' , '%1$s = current page, %2$s = all pages' , 'pietergoosen' );
            echo "<div class="pagination"><span>" . sprintf( $string, $paged, $pages ) . "</span>";
              if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href="".get_pagenum_link(1)."">" . __( '&laquo; First', 'pietergoosen' ) . "</a>";
             if($paged > 1 && $showitems < $pages) echo "<a href="".get_pagenum_link($paged - 1)."">" . __( '&lsaquo; Previous', 'pietergoosen' ) . "</a>";

                for ($i=1; $i <= $pages; $i++)
                {
                   if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
                   {
                        echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href="".get_pagenum_link($i)."" class=\"inactive\">".$i."</a>";
                   }
               }

               if ($paged < $pages && $showitems < $pages) echo "<a href="" . get_pagenum_link($paged + 1)."">" . __( 'Next &rsaquo;', 'pietergoosen' ) . "</a>";
               if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href="".get_pagenum_link($pages)."">" . __( 'Last &raquo;', 'pietergoosen' ) . "</a>";
               echo "</div>\n";
         }
        } //  pietergoosen_pagination

endif;

You can just call the pagination in your template as follows

 <?php  pietergoosen_pagination(); ?>

EDIT

As far as I can gather from your comment to the other answer

i don’t want that page to be my homepage. I have another template for my homepage. I just want it to work and list posts under the www.mywebsite.com/blog/ URL

you have set your blog page as a “Posts page:” in “Reading Settings” and some other page a your “Front page:”. If this is the case, then index.php will be used as your posts page, and not your custom page template.

There is a quick way to check which template is used by the page you are viewing. Just paste the following code in your functions.php (taken from here)

add_action('wp_head', 'show_template');
function show_template() {
    global $template;
    print_r($template);
} 

EDIT 2

Here is one of my modified templates. Please test it and see it works. If this doesn’t work, then I don’t know. Have tested your template in as many ways as possible thinkable, and I can’t get it to fail as you describe

<?php
/**
 * Template Name: Test Page 3
 */
get_header(); ?>

<div id="main-content" class="main-content">

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

        <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 
                global $post;

                $tmp_post = $post;

                 $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

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


                    $wp_query= null;
                    $wp_query = new WP_Query();
                    $wp_query->query( $args );

        if ( $wp_query->have_posts() ) :

                while ( $wp_query->have_posts() ) : $wp_query->the_post();


                    get_template_part( 'content', get_post_format() );

            endwhile;

                if(function_exists('wp_paginate')) {
                wp_paginate();
            }   

                else 

                    get_template_part( 'content', 'none' );

                endif; 

                $post = $tmp_post; 

                 if ( comments_open() || get_comments_number() ) {
                        comments_template();
                    } ?>

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

    <?php get_sidebar(); ?>

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

<?php
get_footer();

Leave a Comment