简体   繁体   English

WordPress获取单个页面/帖子的作者姓名

[英]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(). 您需要首先检查您是否从get_the_author_meta()获得了正确的值。 So, you will know if the problem come from the trim/condition part, or from wordpress itself. 因此,您将知道问题是来自修整/条件部分还是来自wordpress本身。 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. 作者的名称实际上是由php在您的HTML页面中显示的,但是它封装在meta标记中,该标记仅在页面的头部使用,并且不会为用户产生任何可见的输出。

Try to use a div tag instead of the meta one, and make sure you are writing inside the body part of your page. 尝试使用div标记而不是meta标记,并确保您在页面的主体部分内书写。

$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 } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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