2017-05-22 122 views
1

我有一個問題,我試圖顯示一個國家的不同類別的自定義帖子,我使用的代碼作品,但是當有一個以上的類別assigend自定義帖子,圖像不顯示。WordPress自定義類別顯示圖像

這裏是我的代碼:

<?php 
 
$terms = get_the_terms(get_the_ID(), 'story_category'); 
 
if ( $terms[0]->slug == "usa-freebies") :?> 
 
<img class="country" width="23" src="http://www.iconarchive.com/download/i47330/icons-land/vista-flags/United-States-Flag-1.ico"> 
 
<?php elseif ($terms[0]->slug == "uk-freebies") : ?> 
 
<img class="country" width="23" src="http://www.iconarchive.com/download/i47324/icons-land/vista-flags/United-Kingdom-Flag-1.ico"> 
 
<?php elseif ($terms[0]->term_taxonomy_id == "698") :?> 
 
<img class="country" width="21" src="https://cdn2.iconfinder.com/data/icons/Siena/256/globe%20blue.png"> 
 
<?php endif; ?>

回答

2

現在,你是顯示了$terms[0]。 0是$terms數組的第一個位置,所以它將始終顯示第一個類別。你將不得不修改您的代碼並運行foreach循環:

<?php 

    $terms = get_the_terms(get_the_ID(), 'story_category'); 

    foreach($terms as $current_term){ 
     if ( $current_term->slug == "usa-freebies") :?> 
     <img class="country" width="23" src="http://www.iconarchive.com/download/i47330/icons-land/vista-flags/United-States-Flag-1.ico"> 
     <?php elseif ($current_term->slug == "uk-freebies") : ?> 
     <img class="country" width="23" src="http://www.iconarchive.com/download/i47324/icons-land/vista-flags/United-Kingdom-Flag-1.ico"> 
     <?php elseif ($current_term->term_taxonomy_id == "698") :?> 
     <img class="country" width="21" src="https://cdn2.iconfinder.com/data/icons/Siena/256/globe%20blue.png"> 
     <?php endif; 
    } 
?> 
+0

你的先生是一個天才要比 – Brian

+0

此外,僅供參考,您可以使用'的print_r($計算)'來看看你的數據的結構。這將有助於你瞭解你的數據 –

+0

非常感謝,當我試圖找出代碼時,我將開始使用它。 – Brian

相關問題