0

I have set up links to the next/previous posts on my single.php template. Currently I am using <?php previous_post_link(); ?> and <?php next_post_link(); ?> which gives me the title of the next/previous posts, but I would also like to be able to display the author of those posts below the title. Is this possible?

1

Yes, you can do that. Change <?php previous_post_link(); ?> and <?php next_post_link(); ?> with the following code:

For previous post:

  <?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 ?>

For next post:

  <?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 ?>

You also can control from which categories WordPress should select previous and next posts. get_previous_post and get_next_post accept two parameters:

  • (bool) $in_same_cat — prev/next posts will be selected from the same category as you current post
  • (string) $excluded_categories — posts related to these categories will be skipped

More details about these parameters you may find here

  • Thank you, that worked a treat! Will accept your answer as soon as it lets me. – user13286 Dec 7 '16 at 20:49
1

You can create a custom function. For this you need to use

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

And define the get_post_link_with_post_author_name() in your active theme's functions.php as :

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

You can use the same function for previous post link with post name and author name. You just need to get the previous post id using get_previous_post() and pass that post id to the above function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.