Home » Wordpress » WordPress custom query pagination

WordPress custom query pagination

Posted by: admin November 30, 2017 Leave a comment

Questions:

I have a WordPress site, where on the main page I list the content from more categories.

My question is, is there a plugin where I can paginate the results from a category? I mean something like $this->plugin_paginate('category_id'); or smth?

Best Regards,

Answers:

If you’re using the standard WordPress loop, even with a query_posts for a category, pagination is automatic with the usual posts_nav_link. Are you trying to paginate for more than one query and more than one category on the same page?

Edit 11/20: I use this in several different places on one page to show the latest post in a category:

<?php
$my_query = new WP_Query('category_name=mycategory&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
<?php endwhile; ?>

That link then goes to a category page that paginates for that category: Category Templates « WordPress Codex

I don’t know how to paginate different categories on the same page. Must be possible. Maybe ask in the WordPress forums.

Questions:
Answers:

This sounds like something that a simple, well-formed query_posts() call can do. I doubt that you’ll even need to rely on a plugin. 🙂

I’m going to assume that you’re familiar with the query_posts() function, so let’s go ahead and use this example as a base:

// let's get the first 10 posts from category ID 3
query_posts('posts_per_page=10&cat=3');
while(have_posts()):the_post();
    // do WordPress magic right here
endwhile;

Now, to get the 11th to 20th posts from category 3 (that is, the NEXT 10 posts), we’ll want to use the [offset] parameter of query_posts():

// let's get the next 10 posts from category ID 3
query_posts('posts_per_page=10&cat=3&offset=10');
while(have_posts()):the_post();
    // do WordPress magic right here
endwhile;

For most purposes, that should be sufficient. However, you did mention that you plan on paginating category post lists from the main page alone? I’m assuming that you mean that you have multiple category post lists on your main page, and all these are paginated independently.

With something like that, it looks like you’ll have to work a bit with Javascript to do the work for you, along with what I illustrated above.

Questions:
Answers:

I believe you could do something like this:

    <?php
        if(isset($_GET['paged'])){
            $page = $_GET['paged']-1;
        }else{
            $page = 0;
        }
        $postsPerPage = 5;
        $theOffset = $page*$postsPerPage;
    ?>
    <?php query_posts(array('posts_per_page' => $postsPerPage, 'cat' => CATEGORIES HERE, 'offset' => $theOffset)); ?>

Questions:
Answers:

Hope this will help you 🙂

<?php
   $args = array(
  'post_type' => 'post',
  'posts_per_page' => 5,
  'paged' => $page,
  );

  query_posts($args);?>
?>