[英]WordPress get author name for singular page/post
我想显示“作者元标记”的作者姓名,我使用下面的代码来做到这一点,但是我总是得到一个空字符串:
$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 } ?>
如何获得当前显示的页面/帖子作者姓名?
谢谢
您需要首先检查您是否从get_the_author_meta()获得了正确的值。 因此,您将知道问题是来自修整/条件部分还是来自wordpress本身。 为此,请添加您的代码:
echo "Here is my result : ".get_the_author_meta('first_name')." ".get_the_author_meta('last_name');
一旦测试完成,我确定您需要编辑您的问题。
将此答案作为调试的一般建议,而不是针对您的问题的解决方案。
好的,我知道了。 作者的名称实际上是由php在您的HTML页面中显示的,但是它封装在meta标记中,该标记仅在页面的头部使用,并且不会为用户产生任何可见的输出。
尝试使用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.