I’ve got this code which simply displays all the posts for a particular author:
<?php
$all_active_tasks = get_posts(array(
'numberposts' => -1,
'offset' => 0,
'post_status' => 'publish',
'author' => '1',
'post_type' => 'post'
)
);
foreach($all_active_tasks as $post) :
$category = get_the_category();
setup_postdata($post);
?>
<div class="the-post">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><?php echo $category[0]->cat_name; ?></p>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
But what I can’t figure out is how to paginate the results to say 10 per page. I’ve looked at the official codex but nothing I tried seemed to work.
Any help is appreciated.
You can use
paged
together withposts_per_page
parameters. WP_Query. You can grab current page like this:and then use it in your query:
In this way if you put
/page/2/
at the end of your url, the query will return the posts from 11 to 20.How to create the pagination itself, you can check these articles:
here and here.
Setting
'numberposts' => -1
means to get all records. You have to set for the first 10 records (0-9 records)And for next 10, (10-19 records)
And for next 10, (20-29 records)
Try this working code
put this function in functions.php file of your active theme
and here is your modified code to work for pagination