2017-07-01 68 views
0

我想根据帖子的Wordpress作者是谁在页面上显示不同的图像。根据帖子作者显示不同的内容

到目前为止,我已经尝试了几个脚本,但都没有工作。任何帮助是极大的赞赏。这是我想要做的。

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

'如果( $ author =='author1')' –

+0

而一个半冒号或两个可能不会出错 – RiggsFolly

回答

0

get_the_author()函数返回作者的显示名称作为字符串。所以你必须简单比较get_the_author()的结果。没有数组或对象作为返回值。

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

如果你想使用的if语句,您可以使用以下方法::

所以我使用的ifswitch而不是与下面的解决方案去

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

哇谢谢你!第一个工作完美,我只能想象第二个。 非常感谢!非常感谢! –