1

I try to add a code to my wordpress blog show previous posts(in a previous page). I try this code but it don't work:

<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'offset' => 1,
    'cat' => -22,
    'paged'=>$paged
);
$recent = new WP_Query($args);
while($recent->have_posts()) : $recent->the_post();
// here some code to view
<?php endwhile; ?>
<div class="navigation"><p><?php posts_nav_link('&#8734;','next page','previous page'); ?></p></div>
<?php wp_reset_query(); ?>

it's doesn't work in http://mydomain.com/page/2

1 Answer 1

4

You set offset param, so paged param is ignored ;)

From Codex:

offset (int) - number of post to displace or pass over. Warning: Setting the offset parameter overrides/ignores the paged

If you want to ignore most recent post in this query, you can achieve it in two ways.

1. Custom pagination.

$ppp = get_option('posts_per_page');  // or any other method to get your posts_per_page

$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'offset' => 1 + ( $paged * $ppp ),
    'cat' => -22,
    'paged'=>$paged
);

2. Ignore first post

$most_recent_post_id = ...;  // get the ID of most recent post
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'post__not_in' => array($most_recent_post_id),
    'cat' => -22,
    'paged'=>$paged
);
3
  • In a page 1 I want to posts would be displayed from second post, What is solution?
    – Ehsan
    Nov 13, 2013 at 11:18
  • Hmm, you want to ignore most recent post, right? Nov 13, 2013 at 11:20
  • yes, Exactly...
    – Ehsan
    Nov 13, 2013 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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