简体   繁体   中英

Author latest posts pagination

I'm having difficulties in adding pagination for custom wordpress query showing latest author's posts from category 'blog'. I found this code somewhere on stackoverflow. Could someone help me/give some tips how to add pagination to this query? I've spent on it couple hours and couldn't came up with something working.

Best for me would be numeric pagination: 1 | 2 | 3 | 4 | so on...

functions.php

function the_latest_author_posts($post) {
    $relatedargs = array(

         'author' => $post->post_author,
         'post__not_in' => array( $post->ID),
         'posts_per_page' => 8,
         'category_name' => 'blog',
    );

    $relatedquery = new WP_Query( $relatedargs );

    while($relatedquery->have_posts()){
         $relatedquery->the_post(); 
         $ID = get_the_ID();
?> 
<div class="post-blog">
<?php
if(has_post_thumbnail()) { 
    $relatedthumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($ID), 'medium', false);
    $relatedthumbnail_large = wp_get_attachment_image_src( get_post_thumbnail_id($ID), 'full', false);
?>

<?php } ?>
<h1><a href="<?php the_permalink(); ?>"><span><?php the_title(); ?></span></a></h1>
<h6><i class="fa fa-clock-o" aria-hidden="true"></i>&nbsp;&nbsp;<?php echo get_the_time('j') . '/' . get_the_time('m') . '/' . get_the_time('Y') . ' '; ?></h6>

<?php
$content = get_the_content();
echo wp_trim_words( $content , '25' ); ?>

</div>

<?php wp_reset_postdata();}}

I call this function in my template in this way:

<?php
if ( have_posts() ) {

while ( have_posts() ) {
    the_post();
    the_latest_author_posts($post);
    }
}
?>

First add this function in your functions

function numeric_pagination($pages = '', $range = 2){  
 $showitems = ($range * 2)+1;  

 global $paged;
 if(empty($paged)) $paged = 1;

 if($pages == '')
 {
     global $wp_query;
     $pages = $wp_query->max_num_pages;
     if(!$pages)
     {
         $pages = 1;
     }
 }   

 if(1 != $pages)
 {
     echo "<div class='pagination'>";
     if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
     if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a>";

     for ($i=1; $i <= $pages; $i++)
     {
         if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
         {
             echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
         }
     }

     if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>&rsaquo;</a>";  
     if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
     echo "</div>\n";
 } }

and use it after your loop

numeric_pagination();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM