如果作者是特定的人,请在 WordPress 中执行某些操作

Do something in WordPress if the author is a specific person

如何查看当前正在查看的 post 的作者是谁?我正在尝试创建一个自定义作者框,其中一个名为 Vivian 的特定作者在显示她的名字时应该有一个特定的 a-tag。其余的应该只有相同的 H3。

我已经尝试在 IF 和 else 语句中使用 the_authorget_the_author_meta 显示名称,但它似乎不起作用。我错过了什么?

<div class="media-body">
    <?php if (the_author('vivian')) { ?>
        <a href="https://example.com/about/"><h3>VIVIAN SOMETHING</h3></a>
    <?php } else { ?>
        <h3>A random title</h3>
    <?php } ?>
    <p><?php the_author_meta('description'); ?></p>
</div>

您可以通过调用 get_the_author_meta() 进行检查,第一个参数是您要比较的作者字段(display_name、first_name、昵称等)

循环内部:

if (get_the_author_meta('display_name') === 'vivian') {
    // do something
}

如果在循环外,传入作者ID

$author_id = get_post_field('post_author', get_queried_object_id());

if (get_the_author_meta('display_name', $author_id) === 'vivian') {
    // do something
}