Morgari
Morgari

Reputation: 555

How to add next_post_link to the following loop

I've tried to add the <?php next_posts_link('Read more »', 10); ?> but it doesn't work with this loop. How can I solve this case? I want to show 10 posts and read more link for the next 10 posts etc. Thanks.

<div id="posts" class="row">

<?php $loop = new WP_Query( array( 'post_type' => 'post',  'orderby' => 'post_id', 'posts_per_page' => '10', 'order' => 'DESC' ) ); ?>

<?php while( $loop->have_posts() ) : $loop->the_post(); ?>


<div class="item col-sm-4">
<div class="well">
<a href="<?php the_permalink(); ?>"><?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
    ?></a>

<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<div class="readmore-wrapper">
    <a class="readmore" href="<?php the_permalink(); ?>">Read more</a>
</div>
</div></div>

<?php endwhile; wp_reset_query(); ?>



</div>

Upvotes: 0

Views: 189

Answers (2)

Peter HvD
Peter HvD

Reputation: 1661

You need to use the paged variable to tell where you are in the paginated list.

Add this line before your loop: $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : '1'; and then add this to your query args: 'nopaging' => false, 'paged' => $paged,

You can then use previous_posts_link( '« Newer Entries' ); and next_posts_link( 'Older Entries »', $query->max_num_pages ); to get your previous/next links before and after the while/endwhile loop respectively.

BTW, your while loop should ideally be wrapped in an if ( $loop->have_posts() ) statement.

Hope that helps

Upvotes: 1

Wakar Ahmad Khan
Wakar Ahmad Khan

Reputation: 507

Try this with pagination.

<?php
$paged = ( isset( $_GET['pg'] ) && intval( $_GET['pg'] ) > 0 )? intval( $_GET['pg'] ) : 1;
query_posts( array( 'post_type' => 'post', 'paged' => $paged, 'posts_per_page' => 10 ) );

?>
<?php if ( have_posts() ) : ?>
    <?php while( have_posts() ) : the_post(); ?>
        <div id="post-<?php echo $post->ID; ?>" <?php post_class(); ?>>
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <div class="post-excerpt">
                <?php the_excerpt(); ?>
            </div>
        </div>
    <?php endwhile; ?>
    <?php if ( $wp_query->max_num_pages > 1 ) : ?>
        <div class="pagination">
            <?php for ( $i = 1; $i <= $wp_query->max_num_pages; $i ++ ) { 
                $link = $i == 1 ? remove_query_arg( 'pg' ) : add_query_arg( 'pg', $i );
                echo '<a href="' . $link . '"' . ( $i == $paged ? ' class="active"' : '' ) . '>' . $i . '</a>';
            } ?>
        </div>
    <?php endif ?>
<?php else : ?>
    <div class="404 not-found">
        <h3>Not Found</h3>
        <div class="post-excerpt">
            <p>Sorry, but there are no more posts here... Please try going back to the <a href="<?php echo remove_query_arg( 'pg' ); ?>">main page</a></p>
        </div>
    </div>
<?php endif;

// Make sure the default query stays intact
wp_reset_query();

Upvotes: 1

Related Questions