2017-01-31 38 views
0

http://104.152.110.248/~baron/category/recipes/ - 我正在PHP/Wordpress檢查以查看帖子是否屬於類別,然後顯示圖標。部分工作

的網站,所以我在做什麼是有這樣,當後(recipie)是在一定的範疇,我想相應的圖標旁出現帖子的標題。 例如,螃蟹蛋糕屬於魚類,所以它應該只顯示標題旁邊的魚圖標。而Filet Mingion只在牛肉類別中,所以它只能顯示牛圖標。

我遇到的問題是,如果有任何與牛肉分配類別的職位,它顯示所有職位的牛肉圖標。雖然我希望它只顯示分配了牛肉類別的帖子。

這是我正在使用的目前的代碼。

<?php 
    if ($all_the_tags); 
    $all_the_tags = get_categories(); 
    foreach($all_the_tags as $this_tag) { 
     if ($this_tag->name == "Spicy") { 
    ?> 
     <img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Chili Pepper Filled.png"> 

    <?php } else if ($this_tag->name == "Chicken") { ?> 

       <img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Chicken Filled.png"> 

    <?php } else if ($this_tag->name == "Vegetables") { ?> 

       <img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Tomato Filled.png"> 

    <?php } else if ($this_tag->name == "Fish") { ?> 

       <img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Fish Food Filled.png"> 

    <?php } else if ($this_tag->name == "Pork") { ?> 

       <img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Pig.png"> 

    <?php } else if ($this_tag->name == "Beef") { ?> 

       <img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Cow Filled.png"> 

    <?php } else {  
      // it's neither, do nothing 
    ?> 
      <!-- not tagged as one or the other --> 
    <? 
    } 
} 
?> 

回答

1

試試這個,下面的代碼替換代碼:

<?php 
     if (has_category("Spicy",get_the_id())) { 
    ?> 
     <img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Chili Pepper Filled.png"> 

    <?php } else if (has_category("Chicken",get_the_id())) { ?> 

       <img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Chicken Filled.png"> 

    <?php } else if (has_category("Vegetables",get_the_id())) { ?> 

       <img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Tomato Filled.png"> 

    <?php } else if (has_category("Fish",get_the_id())) { ?> 

       <img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Fish Food Filled.png"> 

    <?php } else if (has_category("Pork",get_the_id())) { ?> 

       <img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Pig.png"> 

    <?php } else if (has_category("Beef",get_the_id())) { ?> 

       <img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Cow Filled.png"> 

    <?php } else {  
      // it's neither, do nothing 
    ?> 
      <!-- not tagged as one or the other --> 
    <? 
    } 
?> 

參考:https://developer.wordpress.org/reference/functions/has_category/

相關問題