【问题标题】:Author latest posts pagination作者最新帖子分页
【发布时间】:2017-03-12 10:29:14
【问题描述】:

我在为自定义 wordpress 查询添加分页时遇到困难,该查询显示来自类别“博客”的最新作者帖子。我在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);
    }
}
?>

【问题讨论】:

    标签: php wordpress pagination author


    【解决方案1】:

    首先在你的函数中添加这个函数

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

    【讨论】:

    • @momen-mohamed 感谢您的回复。我已将您的代码添加到 functions.php 并在我的模板中这样写:&lt;?php if ( have_posts() ) { while ( have_posts() ) { the_post(); the_latest_author_posts($post); } } ?&gt; &lt;?php numeric_pagination();?&gt; 但它没有显示分页
    • 试试这个&lt;?php if ( have_posts() ) : while ( have_posts() ) : the_post(); the_latest_author_posts($post); endwhile; numeric_pagination(); endif; ?&gt; 如果这不起作用,我认为问题出在 the_latest_author_posts 函数中
    猜你喜欢
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多