我遇到了问题,我试图在一个类别中的不同自定义帖子上显示国家/地区标志,我正在使用的代码有效,但是当自定义帖子有超过1个类别时,图像不会显示。
这是我的代码:
<?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; ?>
&#13;
答案 0 :(得分: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;
}
?>