Here’s another story for my Custom Pagination on Custom Author’s Page. What I want to happen there is display the Author’s avatar, description, website, and some additional fields, and of course list his post and paginate it….
Here’s the screenshot of my custom author page:
As you can see, everything seems to be in order. Now, the problem is the pagination. I used the code by Kreisi (Pagination w/o using plugins). What I did is create a custom loop and call it to Kreisi’s function.
Here’s the code:
$uid = $curauth->ID;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$authloop = new WP_Query("author=$uid&paged=$paged");
$ppp = 5; //set my custom number of post to appear
$args = array(
'post_per_page' => $ppp,
'author' => $uid,
'paged' => $paged
);
$authorposts = get_posts($args);
if ( count( $authorposts ) > 0 ) {
foreach ( $authorposts as $post ): setup_postdata($post)
?>
<li>
<?php if ( has_post_thumbnail() ) : ?>
<img src="<?php echo bloginfo('template_url'); ?>/inc/scripts/timthumb.php?src=<?php echo catch_that_image(); ?>&w=40&h=40&zc=1&q=30" alt="<?php the_title(); ?>" class="authorpostimg"/>
<?php else : ?>
<img src="<?php echo bloginfo('template_url'); ?>/inc/scripts/timthumb.php?src=<?php echo catch_that_image(); ?>&w=40&h=40&zc=1&q=100" alt="<?php the_title(); ?>" class="authorpostimg"/>
<?php endif; ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>" class="authorpostlink"><?php the_title(); ?></a>
<?php the_excerpt(); ?>
</li>
<?php
endforeach;
wnw_authpagination($authloop->max_num_pages);
} else {
echo '<p>No articles by this user</p>';
}
Here’s the scenario for further understanding.
Supposed the blog has a total of 30 blog posts, and the author Ven Francis has 15. If you can see on my variable, I did set the post_per_page value to 5. And on Kreisi’s function it has the range of 2. (please visit the link to his code to further figure it out) So what it has to happen is that, pagination should show 5 page-buttons
[c] [2] [3] [>] [>>]
where [c] is the current… [3] and [>>] is the last page(3rd page). But what is happening on my situation is that, when you hover the [>>] it shows /page/6 and I think it’s getting the total number of posts of the blog.
And if you click on 3(3rd page). It will still show the 4,5 and [>>] and it should not. Like the illustration below:
[<<] [<] [1] [2] [c] [4] [5] [>] [>>]
I hope you understand the scenario and the problem. Thanks again and God bless!
PS: kreisi’s not doing a support for his brilliant code anymore, so i gotta do this on my own. an i need a little help from you guys. Thank you!
Try my (free & open source) “Easy Pagination Deamon”. It’s pretty advanced and should work on author templates too. Just stick with the readme presented on the link and works for paginated posts & pages too. You can also integrate it in your theme folder and call it in your functions.php with something like
include_once( 'pagination.class.php' );
and then call it in your tempalte withoxo_pagination( array( 'your arguments' ) );
.You are creating two extra loops here:
And:
You use the
$authloop
to get the total number of pages, but you don’t specify theposts_per_page
value so it will take a default value there, which may not be the same as the 5 you specify in the actual post loop you use.Next to that, most of the times it is a bad idea to use a custom loop with pagination. The problem is that WordPress will always do a query for you (to set up the main loop), and it will take the
paged
parameter from the URL with it. Say you are on an author archive. The defaultposts_per_page
is 10. This author has 40 posts. Your custom loop shows only 5 posts per page. If you now are at page 5, WordPress will query for posts 41-50 instead of 21-25 as you would do in your custom loop. WordPress will find no posts and load the404.php
template instead of yourauthor.php
template.For this reason, it’s best not to do a custom loop, but hook into the default loop via
pre_get_posts
or another hook.