I’m trying to simply display the name and description of the post author on my single.php page through the get_the_author_meta() function but I can only get it to return a value when I fill the id field with a number what is not ideal as the author changes depending on the post. I really don’t understand why wordpress isn’t getting the current post author’s id by default. Can someone help me?
Here’s the code I’m using
<div class="col-8 col-md-10">
<?php $id=get_the_author_meta('ID'); echo $id ?>//getting the author's id this way doesn't work
<h2><?php echo get_the_author_meta('nickname',1);?></h2>//this works but only because I filled the id field with a valid number
<p><?php echo get_the_author_meta('user_description') ?></p>//this doesn't workalso
</div>
2
Answers
Managed to solve it using get_post_field( 'post_author' ). I'm not in the Loop but in a regular post so I needed to use this function to get the author's id and from that I managed to get the the rest working
Here's the code
From the docs on
get_the_author_meta()
:Based on your sharing that it won’t work without the user ID, I suspect you are not inside The Loop. Without seeing more of your
single.php
template, it’s hard to say, so if you can share more, that would help.