是否以分页方式显示下一篇文章和上一篇文章的作者?

时间:2016-12-07 作者:user13286

我已经在我的single.php 样板目前我正在使用<?php previous_post_link(); ?><?php next_post_link(); ?> 这给了我下一篇/上一篇文章的标题,但我也希望能够在标题下显示这些文章的作者。这可能吗?

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

是的,你可以做到。改变<?php previous_post_link(); ?><?php next_post_link(); ?> 使用以下代码:

对于以前的职位:

  <?php
    $prev_post = get_previous_post();
    $prev_user = get_user_by( \'id\', $prev_post->post_author );
    if (!empty( $prev_post )): ?>
      <a href="<?php echo $prev_post->guid ?>"><?php echo $prev_post->post_title ?> (<?php echo $prev_user->first_name . \' \' . $prev_user->last_name; ?>)</a>
    <?php endif ?>
下一篇文章:

  <?php
    $next_post = get_next_post();
    $next_user = get_user_by( \'id\', $next_post->post_author );
    if (!empty( $prev_post )): ?>
      <a href="<?php echo $next_post->guid ?>"><?php echo $next_post->post_title ?> (<?php echo $next_user->first_name . \' \' . $next_user->last_name; ?>)</a>
    <?php endif ?>
您还可以控制WordPress应该从哪些类别中选择上一篇和下一篇文章。get_previous_postget_next_post 接受两个参数:

  • (bool) $in_same_cat &mdash;上一篇/下一篇文章将从与当前文章相同的类别中选择(string) $excluded_categories &mdash;将跳过与这些类别相关的帖子find here

SO网友:Aftab

您可以创建自定义函数。为此,您需要使用

// Add in single.php where you want to show next icon.
$nextPost = get_next_post();    // Get the next post detail
$nextPostID = $nextPost->ID;    // Get the post id of next post and pass it 
echo get_post_link_with_post_author_name( $nextPostID );
并定义get_post_link_with_post_author_name() 在活动主题的功能中。php组件:

// This function returns postname, post author and link to it.
function get_post_link_with_post_author_name( $postID ){
    // you have got the postID , now you need to fetch the post name, post url and post author
   $post = get_post( $postID );      
   $post_title = $post->post_title;
   $post_author_id = $post->post_author;
   $author_detail = get_user_by( \'ID\', $post_author_id );
   $post_author_name = $author_detail->display_name;
   $post_url = get_permalink( $postID );
   $respone = "<a href=\'$post_url\'>$post_title >> By $post_author_name</a>";
   return $response;
}
您可以将相同的功能用于具有文章名和作者名的上一篇文章链接。您只需使用get_previous_post() 并将该post id传递给上述函数。

相关推荐

Author post count in category

我想统计特定类别中的作者帖子数量。我该怎么做?我把这根线染红了,但还是洗不出来。Count number of posts by author in a category编辑:这是我得到并尝试过的,但根本不起作用。$user_id = get_the_author_meta(\'ID\') $args = array( \'author_name\' => $user_id, \'category_name\' => \'categorynam