Wordpress自定义帖子类型显示类别的图像

时间:2017-04-13 00:53:25

标签: php wordpress categories

我一直试图让某些东西起作用,我有wordpress,还有自定义的帖子类型和自定义分类,基本上我希望它在某个帖子上显示特定类别的图像,但其余的将显示不同的图像

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

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

1 个答案:

答案 0 :(得分:1)

这将解决您的问题:

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

享受!