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.

This question already has an answer here:

Any tips on fitting pagination into my loop below and I owe you a beer or two. I've worked out how to pull the top rated articles by vote from a custom plugin, and lay it out with a count, but pagination has me stumped. I'm a self-taught learner here so any help would be greatly appreciated.

I was aiming for 6 posts shown per page then use pagination if needed. Two posts are shown and after that a new code section is inserted with a count, the count works, everything works except pagination.

Not sure how this fits in

'$wp_query->query('showposts=6'.'&paged='.$paged); 

when I already use

DESC LIMIT 6 OFFSET 0

in my query...

<?php global $wpdb;
$query_sql = "SELECT like_pid FROM " . $wpdb->prefix ."likes_count ORDER BY like_count DESC LIMIT 6 OFFSET 0";
$query_result = $wpdb->get_col( $wpdb->prepare ($query_sql, OBJECT));
if ($query_result) {
foreach ($query_result as $post_id) {
$post = &get_post( $post_id );
setup_postdata($post); ?>

<?php $count++;
if ($count%2== 0) : ?>

<-- do the loop, count out two articles -->

<?php else : ?>

<-- do something after every two articles -->

<?php endif;?>

<?php } ?>
<?php } ?> //ugly but works

<div class="next"> <?php next_posts_link('&raquo;' ,0); ?></div>
<div class="previous"> <?php previous_posts_link('&laquo;' ,0); ?></div>
share|improve this question

marked as duplicate by Chip Bennett, kaiser, toscho Oct 31 '13 at 13:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

This should get the offset for you, and from there it's a simple matter of adjusting your query

$offset = "0";
$no_of_posts = the_posts_per_page( false ); //Number of posts to display on each page
if (preg_match('/page/', $_SERVER['REQUEST_URI'])) {
    $uri = explode('/', $_SERVER['REQUEST_URI']);
    foreach ($uri as $key=>$value) {
        if ($value == "") {
        unset($uri[$key]);
        }
    }
    $offset = (array_pop($uri) * $no_of_posts) - $no_of_posts;
}
share|improve this answer

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