开发者

WordPress: get author info from post id

开发者 https://www.devze.com 2023-01-05 18:15 出处:网络
Or even the author id from the post id. I am trying to return the author meta (auth开发者_JAVA技巧or page link and avatar) in the sidebar of a single post page (outside of the post loop). What is the

Or even the author id from the post id. I am trying to return the author meta (auth开发者_JAVA技巧or page link and avatar) in the sidebar of a single post page (outside of the post loop). What is the best way to do this? I am using a custom function (see below) to return the post id, but am not sure what function to call next.

function this_post_id() {
  global $wp_query;
  $thePostID = $wp_query->post->ID;
  return $thePostID;
}


I figured it out.

<?php $author_id=$post->post_author; ?>
<img src="<?php the_author_meta( 'avatar' , $author_id ); ?> " width="140" height="140" class="avatar" alt="<?php echo the_author_meta( 'display_name' , $author_id ); ?>" />
<?php the_author_meta( 'user_nicename' , $author_id ); ?> 


If you want it outside of loop then use the below code.

<?php
$author_id = get_post_field ('post_author', $post_id);
$display_name = get_the_author_meta( 'display_name' , $author_id ); 
echo $display_name;
?>


**use this code for display Auth Name**

<?php
$auth_id = $post->post_author; 
echo get_the_author_meta( 'display_name', $auth_id ); 
?>


<?php
  $field = 'display_name';
  the_author_meta($field);
?>

Valid values for the $field parameter include:

  • admin_color
  • aim
  • comment_shortcuts
  • description
  • display_name
  • first_name
  • ID
  • jabber
  • last_name
  • nickname
  • plugins_last_view
  • plugins_per_page
  • rich_editing
  • syntax_highlighting
  • user_activation_key
  • user_description
  • user_email
  • user_firstname
  • user_lastname
  • user_level
  • user_login
  • user_nicename
  • user_pass
  • user_registered
  • user_status
  • user_url
  • yim


This should work

  • Post ID
$post_id =  get_the_ID();
$author_id = get_post_field ('post_author', $post_id);
$display_name = get_the_author_meta( 'nickname' , $author_id ); 
echo $display_name;

Note: You need to use this function inside the loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号