【问题标题】:WordPress get author name for singular page/postWordPress获取单个页面/帖子的作者姓名
【发布时间】:2016-08-06 22:08:57
【问题描述】:

我想显示“作者元标记”的作者姓名,我使用以下代码来执行此操作,但我总是得到一个空字符串:

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

如何获取当前显示的页面/帖子作者姓名?

谢谢

【问题讨论】:

  • 你的问题解决了吗?

标签: php wordpress meta-tags meta author


【解决方案1】:

您需要先检查您是否从 get_the_author_meta() 中获得了正确的值。因此,您将知道问题是来自修剪/条件部分,还是来自 wordpress 本身。 为此,请添加您的代码:

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

完成此测试后,我确定您需要编辑您的问题。

将此答案作为调试的一般建议,而不是解决您的问题。

【讨论】:

  • 嗨,这些值也不会返回任何内容
  • 检查wordpres数据库,有作者姓名吗?
  • 是的,确实存在
【解决方案2】:

好的,知道了。作者的名字实际上是通过php在你的HTML页面中显示的,但是它被封装在一个meta标签中,这个标签只用在你页面的head部分,不会为用户产生任何可见的输出。

尝试使用 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 } ?>

【讨论】:

    猜你喜欢
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多