【问题标题】:How do I hide the tag-container when there's no tags?没有标签时如何隐藏标签容器?
【发布时间】:2013-07-14 21:50:45
【问题描述】:

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

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

【问题讨论】:

    标签: php wordpress if-statement tags hide


    【解决方案1】:

    除了其他答案之外,我会将呈现的标签存储在一个变量中,以免两次调用同一个函数。

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

    【讨论】:

    • 如果他需要标签正确显示,则需要对标签进行内爆和链接,这将简单地将Array 显示为值:) 类似于'&lt;a href="..."&gt;'.implode('&lt;/a&gt;, &lt;a href="..."&gt;', $tags) . '&lt;/a&gt;'
    • get_the_tags 已经调用了标签是否重要? :)
    • @michaelw90 这是一种速度优化策略; [at]ChenAsraf 抱歉,没看到 - 感谢您的提示。 (但在该架构之后还有许多其他 Wordpress 方法)
    【解决方案2】:
    <?php
    if( get_the_tags() ){
        echo '<div class="tag-container"><p>';
        the_tags();
        echo '</p></div>';
    }
    

    【讨论】:

    • 太好了,效果很好:) 也可以使用 get_the_excerpt 来完成吗?
    【解决方案3】:
    <?php if (!empty(get_the_tags())) : ?>
    <div class="tag-container">
        <p><?php the_tags(); ?></p>
    </div>
    <?php endif; ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多