Why The Pagination Doesn’t Work In The Custom Blog Posts Page?

WordPress

Custom Blog Posts Page

When I customized Twenty Twelve theme for my personal blog, I wanted to create a custom blog posts page template to show the posts in a full-width page,  it’s so easy I thought, just did the things step by step.

Copy index.php as a new file, rename it to template-fullwidth-blog.php, add /Template Name: Full-width Blog/ into the file, add the following codes in front of while while (have_posts() ) : the_post();

<?php
$limit = get_option('posts_per_page');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('post_type=post&posts_per_page='.$limit.'&paged='.$paged);
?>

And then, create a new page with this template,  save the page and go to “Settings > Reading”, set the front page to this static page, finished! It’s very easy, isn’t it?

But, I drawn up sharply by an unexpected question, the pagination doesn’t work in the homepage, but it was working properly when it’s not the front page, it was so weird!

I was googling many tutorials and following their methods,  but it still no luck. So, I compared those solutions and my codes, finally I found the reason, I missed a very small detail, the trouble is the following codes

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

When the page is the static front page, it should be changed to:

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

do you find the difference? Yes, change get_query_var(‘paged’) to get_query_var(‘page’)

So the final solution as below:

<?php
         $limit = get_option('posts_per_page');
         if(is_home()){
                $paged = (get_query_var('page')) ? get_query_var('page') : 1;
             }else{
           $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
             }
            query_posts('post_type=post&posts_per_page='.$limit.'&paged='.$paged);
?>

If you are using our free theme ‘badjohnny‘, you can learn the entire code in /page-templates/blog-single-column.php file.  I hope this tip can help the guys who is confusing the same question!

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

×