简体   繁体   English

WordPress自定义类别显示图像

[英]WordPress Custom Category Show image

I Have a Problem, I am trying to show a country Flag on different custom posts in a category, the code i am using works but when there is more than 1 category assigend to a custom post, the images do not show. 我有一个问题,我试图在类别中的不同自定义帖子上显示一个国家标志,我正在使用的代码有效,但是当一个自定义帖子有多个类别时,图像将不会显示。

Here is my code: 这是我的代码:

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

Right now, you are showing the $terms[0] . 现在,您正在显示$terms[0] 0 is the first position of $terms array so it will always show the first category. 0是$terms数组的第一个位置,因此它将始终显示第一个类别。 You will have to modify your code and run a foreach loop : 您将必须修改您的代码并运行一个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; 
    }
?>

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

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