【问题标题】:Hide parent div if child div doesn't have content如果子 div 没有内容,则隐藏父 div
【发布时间】:2018-09-22 01:25:31
【问题描述】:

如果孩子没有标签,我会尝试在 Wordpress 中隐藏父元素。 Т这是代码:

<div id="tags">
   <h4 class="tag-title">Tags</h4>
    <?php the_tags( '', ' ', '<br />' ); ?>
</div>

如果 php 字符串为空,我想隐藏所有 div #tags。

【问题讨论】:

    标签: wordpress tags hide parent


    【解决方案1】:

    您可以使用has_tag()函数来检查是否有标签。

    <?php if(has_tag()) { ?>
    <div id="tags">
       <h4 class="tag-title">Tags</h4>
        <?php the_tags( '', ' ', '<br />' ); ?>
    </div>
    <?php } ?>
    

    【讨论】:

      【解决方案2】:

      如果您使用 html 元素作为 the_tags 的 $before、$sep 和 $after 参数,则可以在只有一个子 (the) 时隐藏 #tags。

      看这个帖子:Selector for when only one child exists in parent我想出了下面的例子。

      <style>
          .tags h4:nth-child(1):last-child{
              display: none;
          }
      </style>
      
      <div class='tags' id='tags1'>
          <h4>Some nonsense</h4>
      </div>
      
      <div class='tags' id='tags2'>
          <h4>Some nonsense</h4>
          <ul><li>Some</li><li>other</li></ul>
      </div>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多