WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using a page template with code to display all blog posts:

<?php /* Template Name: Blog */ ?>

<?php get_header(); ?>

<div id="main">

// ACF Page number variable
$numposts = get_field('number_of_posts_per_page');
$args = array( 'numberposts' => $numposts );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post);

    <div class="blogpost clearfix">
    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    <span class="postdetails">
    <?php the_time ('j F, Y'); ?>&nbsp;&nbsp;<span class="bullet">&bull;</span>&nbsp;&nbsp;Posted by:&nbsp;<?php the_author(', '); ?>&nbsp;in:&nbsp;<?php the_category(', '); ?><?php the_tags( '&nbsp;&nbsp;<span class="bullet">&bull;</span>&nbsp;&nbspTags: ', ', ', '<br />' ); ?>
    <?php the_content(); ?>
<?php endforeach; ?>

<?php wp_pagenavi(); ?>

<?php get_footer(); ?>

The problem I'm having is I cannot get any pagination to work. Above I have pagenavi installed but nothing appears. I have set the post limit to 5 in both my custom field (which shows 5, there are 6 posts) and in WP settings.

Can anyone shed any light? Thanks.

share|improve this question
I believe it was my loop that sucked, this is fixed, thanks. – Phill Mar 4 '12 at 6:01
Post your solution for anyone who comes after you to use. – mor7ifer Mar 4 '12 at 11:57

probably coz you override the default query and forgot to take care of pagination :-) change the code this way:

$page = isset(get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'numberposts' => $numposts, 'paged' =>  $paged );

This should do the work

share|improve this answer

I have to say it's unusual to see someone using a custom template for a blog considering the index.php is for this; I guess it depends on your set up. I'd suggest being careful whenever doing custom queries.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.