最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugins - How to get current post user id

matteradmin10PV0评论

I have some posts, each post have different authors(users).
I save each author image avatar in user_meta table.
Now i want show each author avatar in self posts. i get user_id with wp_get_current_user() function. but it work when user is logged in, i want when user not logged in get user id and show user avatar

Here my code

first way show user post avatar to all post not work $current_user = $post->post_author;

  <?php if (!empty(get_user_meta($current_user, 'user_avatar', true))): ?>
                        <img src="<?php echo get_user_meta($current_user, 'user_avatar', true); ?>" alt="some text">

Second way work, but whene user is logged in

$current_user = wp_get_currebt_user();

  <?php if (!empty(get_user_meta($current_user->ID, 'user_avatar', true))): ?>
                        <img src="<?php echo get_user_meta($current_user->ID, 'user_avatar', true); ?>" alt="some text">

I have some posts, each post have different authors(users).
I save each author image avatar in user_meta table.
Now i want show each author avatar in self posts. i get user_id with wp_get_current_user() function. but it work when user is logged in, i want when user not logged in get user id and show user avatar

Here my code

first way show user post avatar to all post not work $current_user = $post->post_author;

  <?php if (!empty(get_user_meta($current_user, 'user_avatar', true))): ?>
                        <img src="<?php echo get_user_meta($current_user, 'user_avatar', true); ?>" alt="some text">

Second way work, but whene user is logged in

$current_user = wp_get_currebt_user();

  <?php if (!empty(get_user_meta($current_user->ID, 'user_avatar', true))): ?>
                        <img src="<?php echo get_user_meta($current_user->ID, 'user_avatar', true); ?>" alt="some text">
Share Improve this question asked Oct 28, 2018 at 5:56 davOOddavOOd 271 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

To show author avatar per post inside loop you need to modify a little:

$current_post_author_id = get_the_author_meta( 'ID' );
<?php if (!empty(get_user_meta($current_post_author_id, 'user_avatar', true))): ?>
<img src="<?php echo get_user_meta($current_post_author_id, 'user_avatar', true); ?>" alt="some text">

Try, if it works for you.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far