作者最新帖子分页

时间:2016-10-29 20:52:28

标签: php wordpress pagination author

我在为自定义wordpress查询添加分页时遇到了困难,显示了来自类别' blog'的最新作者帖子。我在stackoverflow上找到了这个代码。有人可以帮助我/给出一些如何为此查询添加分页的提示吗?我已经花了几个小时,却无法想出一些有用的东西。

最适合我的是数字分页: 1 | 2 | 3 | 4 |等......

的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();}}

我以这种方式在我的模板中调用此函数:

<?php
if ( have_posts() ) {

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

1 个答案:

答案 0 :(得分:0)

首先在函数中添加此函数

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";
 } }

并在循环后使用它

numeric_pagination();