2017-07-01 71 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

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