Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using WP3.5, and I have a post_list.php which will show all the posts order by post date DESC. Everything is ok except pagination.

I have 15 posts, for test purpose, I want to every page show 2 posts, others will be show next page like this.

Prev 1 2 3 4 5 Next

Some parts of post_list.php

define('WP_USE_THEMES', false);
require_once '../blog/wp-load.php';

$query_args = array(
    'orderby' => 'date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 2,
    'offset' => 0

);
$lateset_posts = get_posts($query_args);

<?php 
global $post;
foreach($lateset_posts as $post) :
    // do something
    the_content();
<?php endforeach;?>

<?php next_post_link(); previous_post_link(); ?>

Yes, as you see, every page show 2 posts as I set, but I cannot get the pagination as I want to. Instead, it shows me like this

Post1's title >> << Post2's title

How could I get the pagination style like below

Prev 1 2 3 4 5 Next

Thanks.

Edit: Now I use paginate_links function, yes, I got what I want.

$query_args = array(
    'orderby' => 'date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 2,
    'offset' => 0

);
$lateset_posts = get_posts($query_args);


$paging_args = array(
    'base'         => '%_%',
    'format'       => '?paged=%#%',
    'total'        => count($lateset_posts),
    'current'      => max(1, get_query_var('paged')),
    'end_size'     => 1,
    'mid_size'     => 1,
    'prev_next'    => True,
    'prev_text'    => __('« Previous'),
    'next_text'    => __('Next »')
);
$lateset_posts_paging = paginate_links($paging_args);

I have 15 rows , and it shows like this

 1 2 3 .. Next

Now I am in the first page, and I want to go to the second page, so I click the number 2, and I notic the url is :

http://localhost/x/post_list.php?paged=2

But after I click it ,it still in the first page, and the data is also the first two rows. Where is am I wrong , Thanks.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Use paginate_links. It is meant to do pretty much just that. Plus, the ordinary pagination functions like next_posts_link (which I think you want instead of next_post_link) do not work reliably with custom queries, but next_post_link and previous_post_link are equally or more troublesome with custom queries.

With paginate_links you need to pass quite a bit of data, so read the Codex carefully.

share|improve this answer
    
thanks s_ha_dum, indeed, I need to read the Codex carefully. Thanks. –  diligent Jun 27 '13 at 14:46
    
hi, I edit my question, after I use this funciton, I found I can not go to the page what I want to go. –  diligent Jun 27 '13 at 15:26
    
Your function is not using the 'paged' parameter that paginate_links is sending. By the way, I'd use some other key than paged as WordPress uses that and you may end up with conflicts. –  s_ha_dum Jun 27 '13 at 15:35
add comment

Yes, I need to add a 'paged' parameter to my get_posts function

$query_args = array(
    'orderby' => 'date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 2,
    'offset' => 0,
    'paged' => get_query_var('paged')

);
share|improve this answer
add comment

Your Answer

 
discard

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.