skip to Main Content

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


  1. Chosen as BEST ANSWER

    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

    <div class="col-8 col-md-10">
            <?php $id=get_post_field( 'post_author' )?>
            <h2><?php echo get_the_author_meta('first_name',$id).' '.get_the_author_meta('last_name',$id);?></h2>
            <p><?php echo get_the_author_meta('user_description',$id) ?></p>
        </div>
    

  2. From the docs on get_the_author_meta():

    If used within The Loop, the user ID need not be specified, it defaults to current post author. A user ID must be specified if used outside The Loop.

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search