如果子 div 没有内容,则隐藏父 div

Hide parent div if child div doesn't have content

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

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

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

如果使用 html 元素作为 the_tags 的 $before、$sep 和 $after 参数,当只有一个 child (the ) .

看着这个 post: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>

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

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