Wordpress 自定义 Post 类型显示图片类别

Wordpress Custom Post type show image for category

我一直在努力让一些东西发挥作用,我有 wordpress,还有自定义 post 类型和自定义分类法,基本上我希望它在单个 post 上显示图像某些类别,但其余类别将显示不同的图像。

这是我的代码任何人都知道为什么这不起作用,它只显示第二张图片。

<?php   // Get terms for post
$terms = get_the_terms( 'story_category' );
if ( $terms == "global-freebies" || $terms == "usa-freebies" || $terms == "uk-freebies" ){ ?>
<center><span class="domain"><a href="<?php echo esc_url( $post_url ); ?>" target="_blank"><img width="250" src="http://kwikfreebies.com/wp-content/uploads/2017/04/freebie_button.jpg"></a></span></center>
<?php } else { ?>
<center><span class="domain"><a href="<?php echo esc_url( $post_url ); ?>" target="_blank"><img width="250" src="http://kwikfreebies.com/wp-content/uploads/2017/04/site_button.jpg"></a></span></center>
<?php } ?>

这将解决您的问题:

<?php $terms = get_the_terms( $post->ID, 'story_category' );
if ( $terms[0]->slug == "global-freebies" || $terms[0]->slug == "usa-freebies" || $terms[0]->slug == "uk-freebies"  ) :?>
<center><span class="domain"><a href="<?php echo esc_url( $post_url ); ?>" target="_blank"><img width="250" src="http://kwikfreebies.com/wp-content/uploads/2017/04/freebie_button.jpg"></a></span></center>
<?php else : ?>
<center><span class="domain"><a href="<?php echo esc_url( $post_url ); ?>" target="_blank"><img width="250" src="http://kwikfreebies.com/wp-content/uploads/2017/04/site_button.jpg"></a></span></center>
<?php endif;?>

尽情享受吧!