【问题标题】:WP Hide parent container when both child fields (acf fields) are empty当两个子字段(acf 字段)为空时,WP 隐藏父容器
【发布时间】:2018-06-18 16:48:16
【问题描述】:

可以提供一些帮助,因为我尝试了一些方法,但没有成功...

我有一个容器 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>

任何帮助都会很棒...

【问题讨论】:

  • 真正的问题是什么——你不知道有什么逻辑运算符,或者什么? (顺便说一句,由于您多次调用这些函数,您应该将结果存储在变量中。)
  • 基本上,不知道该怎么做。感谢您提供有关变量的提示

标签: php wordpress advanced-custom-fields


【解决方案1】:

可能的解决方案:

<?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; ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 2020-12-21
    • 2013-10-02
    • 2013-09-03
    • 1970-01-01
    • 2020-07-17
    • 2016-06-17
    相关资源
    最近更新 更多