WP当两个子字段(acf字段)为空时隐藏父容器

时间:2018-01-09 15:42:15

标签: php wordpress advanced-custom-fields

可以提供一些帮助,因为我尝试了一些但没有运气......

我有一个容器div,里面有两个字段(wordpress acf)。我可以显示字段,如果他们有内容,他们不显示如果是空的。我现在需要的是显示容器div,如果一个或两个字段都有内容,或者如果两个字段都为空,则显示容器字段。

当前代码

<div class="header-contact">    
<?php if( get_field('header_tel', 'option') ): ?>

                        <p>Tel No: <a href="tel:<?php the_field('header_tel', 'option'); ?>"><?php the_field('header_tel', 'option'); ?></a></p>

                    <?php endif; ?>

                    <?php if( get_field('header_email', 'option') ): ?>
                    <a href="mailto:<?php the_field('header_email', 'option'); ?>"><?php the_field('header_email', 'option'); ?></a>
                    <?php endif; ?>
</div>

任何帮助都会很棒......

1 个答案:

答案 0 :(得分:2)

可能的解决方案:

<?php if( get_field('header_tel', 'option') || get_field('header_email', 'option') ): ?>
   <div class="header-contact"> 
     <?php if( get_field('header_tel', 'option') ): ?>
       <p>Tel No: <a href="tel:<?php the_field('header_tel', 'option'); ?>"><?php the_field('header_tel', 'option'); ?></a></p>
     <?php endif; ?>
     <?php if( get_field('header_email', 'option') ): ?>
       <a href="mailto:<?php the_field('header_email', 'option'); ?>"><?php the_field('header_email', 'option'); ?></a>
     <?php endif; ?>
   </div>
<?php endif; ?>
相关问题