按作者函数分页获取相关帖子

时间:2014-12-16 作者:bpy

是否可以将此函数分页?我已经尝试添加上一个和下一个链接,但I\'ts not working。。。

function get_related_author_posts() {
            global $authordata, $post;

            $authors_posts = get_posts( array( \'author\' => $authordata->ID, \'post_type\' => \'kenniscentrum\', \'post__not_in\' => array( $post->ID ), \'posts_per_page\' => 5 ) );

            $output = \'<ul>\';
            foreach ( $authors_posts as $authors_post ) {
                $output .= \'<li><a href="\' . get_permalink( $authors_post->ID ) . \'">\' . apply_filters( \'the_title\', $authors_post->post_title, $authors_post->ID ) . \'</a>\' . apply_filters( \'the_excerpt\', $authors_post->post_excerpt, $authors_post->ID ) . \'</li>\';
            }
            $output .= \'</ul>\';
      // bof previous and next links adde by me not working
            $output .= \'<div class="nav-previous"> \'.next_posts_link( __( \'<span class="meta-nav">&larr;</span> Older posts\' ) ) .\'</div>\';
            $output .= \'<div class="nav-next"> \'.previous_posts_link( __( \'Newer posts <span class="meta-nav">&rarr;</span>\' ) ) .\'</div>\';
     // eof previous and next links adde by me not working
            return $output;

        }

        /*add this hook where you need the related posts*/
        if(get_related_author_posts()) :
            echo get_related_author_posts();
        else :    
            echo \'No articles...\';
        endif;  

3 个回复
最合适的回答,由SO网友:bpy 整理而成

而不是放置在函数上的函数。php,我找到了一个可以放在模板文件上的解决方法:

<?php  
global $authordata, $post;
 query_posts( array( 
\'author\' => $authordata->ID, 
\'post_type\' => \'post\', 
\'post__not_in\' => array( $post->ID ), 
\'paged\'         => $paged,
\'posts_per_page\' => 5 ));
if (have_posts()) : while (have_posts()) : the_post(); ?>

<p><a href="<?php the_permalink(); ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>

                    <?php endwhile; ?>
              <div id="nav-below" class="navigation">
              <?php wp_pagenavi(); ?>
              </div><!-- #nav-below -->
              <?php endif; wp_reset_query(); ?> 

SO网友:Pieter Goosen

正如我在对your answer, 你不应该利用query_posts

Note: 此功能不适用于插件或主题。如后文所述,有更好、性能更好的选项来更改主查询。query\\u posts()是一种过于简单且有问题的方法,通过将页面的主查询替换为新的查询实例来修改它。它效率低下(重新运行SQL查询),并且在某些情况下会彻底失败(尤其是在处理POST分页时)。

当您需要分页查询时,WP_Query 是一种方法,因为它返回了正确计算所需的所有信息,以便分页正常工作。使用WP_Query 结果被缓存,使其速度更快

您必须在这里做两件事:

设置paged 查询参数中的参数

设置$max_pages 中的参数next_posts_link()

你可以试试这样的方法:(注意:这是未经测试的)

function get_related_author_posts() {

    global $authordata, $post;

    $paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;
    $args = array( 
        \'author\'            => $authordata->ID, 
        \'post_type\'         => \'kenniscentrum\', 
        \'post__not_in\'      => array( $post->ID ), 
        \'posts_per_page\'    => 5,
        \'paged\'             => $paged
    );
    $authors_posts = new WP_Query( $args );

    $output = \'\';

    if( $authors_posts->have_posts() ) {

        $output = \'<ul>\';

        while( $authors_posts->have_posts() ) {
            $authors_posts->the_post();

            $output .= \'<li><a href="\' . get_permalink() . \'">\' . get_the_title() . \'</a>\' . get_the_excerpt() . \'</li>\';

        }

        $output .= \'</ul>\';

        $output .= \'<div class="nav-previous"> \'. get_next_posts_link( __( \'<span class="meta-nav">&larr;</span> Older posts\' ), $authors_posts->max_num_pages) .\'</div>\';
        $output .= \'<div class="nav-next"> \'. get_previous_posts_link( __( \'Newer posts <span class="meta-nav">&rarr;</span>\' ) ) .\'</div>\';


        wp_reset_postdata();
    }

    return $output;

}

SO网友:Karun

在之后的代码中global $authordata, $post; 添加

$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
并更改您的代码

$authors_posts = get_posts( array( \'author\' => $authordata->ID, \'post_type\' => \'kenniscentrum\', \'post__not_in\' => array( $post->ID ), \'posts_per_page\' => 5 ) );

$authors_posts = get_posts( array( 
    \'author\' => $authordata->ID, 
    \'post_type\' => \'kenniscentrum\', 
    \'post__not_in\' => array( $post->ID ), 
    \'paged\' => $paged,
    \'posts_per_page\' => 5 ) 
);
您的功能现在应该可以在分页时正常工作。

结束

相关推荐

Pagination and Related Posts

是否可以对嵌入在single中的以下相关POST代码使用分页。我的主题的php。 <?php // for use in the loop, list 5 post titles related to first tag on current post $tags = wp_get_post_tags($post->ID); if ($tags) { $first_tag = $