简体   繁体   中英

WordPress get author name for singular page/post

I want to display author name for "author meta tag" , I used following code to do this, But I always get an empty string for that:

$fname = get_the_author_meta('first_name');
$lname = get_the_author_meta('last_name');
$author = trim( "$fname $lname" );
if ( $author ) { ?>
    <meta name="author" content="<?php echo $author; ?>">
<?php } ?>

How can I get the current displayed page/post author name ?

Thank you

You need to check first you if you get correct values from the get_the_author_meta(). So, you will know if the problem come from the trim/condition part, or from wordpress itself. To do this, add in your code :

echo "Here is my result : ".get_the_author_meta('first_name')." ".get_the_author_meta('last_name');

Once this test done, i'm sure you'll need to edit your question.

Take this answer as a general advice for debugging, more than a solution for your problem.

Ok, got it. The name of the author is actually displayed by php in your HTML page, but it's encapsulated in a meta tag, which is only used in the head part of your page, and don't produce any visible output for the user.

Try to use a div tag instead of the meta one, and make sure you are writing inside the body part of your page.

$fname = get_the_author_meta('first_name');
$lname = get_the_author_meta('last_name');
$author = trim( "$fname $lname" );
if ( $author ) { ?>
    <div>Author: <?php echo $author; ?></div>
<?php } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM