简体   繁体   English

根据帖子作者显示不同的内容

[英]Display different content depending on post author

I am trying to display a different image on my page depending on who the Wordpress author of the post is. 我试图根据帖子的Wordpress作者是谁在页面上显示其他图像。

So far I have tried a few scripts but none of them work. 到目前为止,我已经尝试了一些脚本,但是没有一个起作用。 Any help is greatly appreciated. 任何帮助是极大的赞赏。 Here is what I am trying to do. 这是我想要做的。

<?php $author = get_the_author(); ?> 
<?php
if ( $author('author1') ) {
    echo ' 
    <img src="">;
    '
} elseif ( $author('author2') ) {
echo '
    <img src="">;
    '
    } else {
    // if neither, echo something else
}
?>

The get_the_author() function return the author's display name as a string. get_the_author()函数以字符串形式返回作者的显示名称。 So you have to simply compare the result of get_the_author() . 因此,您只需要简单比较get_the_author()的结果。 There is no array or object as return value. 没有数组或对象作为返回值。

So I would go with the following solution using a switch instead of if : 所以我将使用switch而不是if使用以下解决方案:

<?php $author = get_the_author(); ?> 
<?php
    switch($author) {
        case 'author1':
            echo '<img src="">';
            break;
        case 'auhtor2':
            echo '<img src="">';
            break;
        default:
            // if neither, echo something else
    }
?>

In case you want to use the if statement you can use the following: 如果要使用if语句,可以使用以下命令:

<?php $author = get_the_author(); ?> 
<?php
    if ($author === 'author1') {
        echo '<img src="">';
    } elseif ($author === 'author2') {
        echo '<img src="">';
    } else {
        // if neither, echo something else
    }
?>

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

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