0

I want to limit 3 blog entry snippets per page and include pagination for additional pages. Here's what I have so far, but pagination isn't working.

            <div id="page-content">

<?php
$args = array( 'posts_per_page' =>3, 'order'=> 'ASC', 'orderby' => 'title' );
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
  setup_postdata( $post ); ?> 
    <div>

        <a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a><br />
        <small><?php the_time('F jS, Y') ?> by <?php the_author(); ?></small>
        <?php the_excerpt(); ?><br />
    </div>
<?php endforeach; 
wp_reset_postdata();?>
      </div>

<?php posts_nav_link( $sep, $prelabel, $nextlabel ); ?> 
<?php posts_nav_link(); ?>
<div class="navigation">
<div class="alignleft"><?php posts_nav_link('','','&laquo; Previous Entries') ?></div>
<div class="alignright"><?php posts_nav_link('','Next Entries &raquo;','') ?></div>
</div>
        <!--#page-content-->
0

The sweet and short of this, don't use get_posts if you need paginated queries. get_posts works perfectly if you are going to use a custom query that doesn't need pagination, but it really becomes a big complicated mess when you need to introduce pagination.

I think the easiest and most appropriate here is to make use of WP_Query to construct your custom query, that is, if you can't use pre_get_posts to alter the main query to get your desired output from the main query.

Also, these two lines are unnecessary, you must delete these

<?php posts_nav_link( $sep, $prelabel, $nextlabel ); ?> 
<?php posts_nav_link(); ?>

I do think that next_posts_link() and previous_posts_link() is better to use with a custom query, ie with WP_Query. You must just remember however to set the $max_pages parameter when you make use of a custom query, otherwise your pagination will break

With a few minor tweaks, your query should look like this

<div id="page-content">

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array( 'posts_per_page' =>3, 'order'=> 'ASC', 'orderby' => 'title', 'paged' => $paged );
    $postslist = new WP_Query( $args );

    if ( $postslist->have_posts() ) :
        while ( $postslist->have_posts() ) : $postslist->the_post(); ?>
            <div>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a><br />
                <small><?php the_time('F jS, Y') ?> by <?php the_author(); ?></small>
                <?php the_excerpt(); ?><br />
            </div>
        <?php endwhile; ?>
        <div class="navigation">
            <div class="alignleft"><?php next_posts_link( '&laquo; Previous Entries', $postslist->max_num_pages ); ?></div>
            <div class="alignright"><?php previous_posts_link( 'Next Entries &raquo;' ); ?></div>
        </div>
        <?php wp_reset_postdata();
    endif;
    ?>

</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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