如果没有标签,如何隐藏标签容器?

时间:2013-07-16 15:49:49

标签: php wordpress if-statement tags hide

我想知道如何才能让#tag-container仅在有标签时显示。我该怎么做呢?我在想有一些if和else语句,但我无法弄清楚如何正确编写它......

<div class="tag-container">
    <p><?php the_tags(); ?></p>
</div>

3 个答案:

答案 0 :(得分:3)

<?php
if( get_the_tags() ){
    echo '<div class="tag-container"><p>';
    the_tags();
    echo '</p></div>';
}

答案 1 :(得分:0)

<?php if (!empty(get_the_tags())) : ?>
<div class="tag-container">
    <p><?php the_tags(); ?></p>
</div>
<?php endif; ?>

答案 2 :(得分:0)

除了其他答案之外,我会将渲染的标签存储在一个变量中,而不是两次调用相同的函数。

<?php $tags = get_the_tag_list( __('Tags: '), ', ' ); ?>
<?php if( !empty( $tags ) ) : ?>
<div class="tag-container">
    <p><?php echo $tags; ?></p>
</div>
<?php endif; ?>