【问题标题】:How to show ACF Sub Field only if the value is set, if not, hide?如何仅在设置值时显示 ACF 子字段,如果没有,隐藏?
【发布时间】:2018-06-16 08:44:49
【问题描述】:

我使用的是 ACF,内容灵活。

我为 facebook、twitter 等创建了媒体字段。

字段会得到链接,因为前端只是公司的标志。

我需要显示html,只有设置了值,否则不显示。

我怎样才能做到这一点?我需要这样的东西:

                       if( get_row_layout() == 'social_media_icons' ): ?>

                            if get_sub_field('media_facebook') is set, show this {
                            <li class="author-profile__social-item"><a rel="noopener noreferrer" href="<?php echo get_sub_field('media_facebook'); ?>" class="author-profile__social-anchor" target="_blank"><i class="fa fa-facebook" aria-hidden="true"></i></a></li> 
                            }

                            if get_sub_field('media_twitter') is set, show this {
                            <li>Twitter</li>
                            }

                        <?php endif; ?>

我试过这个:

  <?php

                while ( have_posts() ) : the_post();

                if( have_rows('social_media','user_'. $author_id) ):

                    while ( have_rows('social_media', 'user_'. $author_id) ) : the_row();

                        if( get_row_layout() == 'social_media_icons' ): ?>

                            <?php if (get_sub_field('media_facebook')) { ?>
                            <li class="author-profile__social-item"><a rel="noopener noreferrer" href="<?php echo get_sub_field('media_facebook'); ?>" class="author-profile__social-anchor" target="_blank"><i class="fa fa-facebook" aria-hidden="true"></i></a></li> 
                            <?php } ?>

                            <?php if (get_sub_field('media_twitter')) { ?>
                            <li class="author-profile__social-item"><a rel="noopener noreferrer" href="<?php echo get_sub_field('media_twitter'); ?>" class="author-profile__social-anchor" target="_blank"><i class="fa fa-twitter" aria-hidden="true"></i></a></li>
                            <?php } ?>

                            <?php if (get_sub_field('media_github')) { ?>
                            <li class="author-profile__social-item"><a rel="noopener noreferrer" href="<?php echo get_sub_field('media_github'); ?>" class="author-profile__social-anchor" target="_blank"><i class="fa fa-github" aria-hidden="true"></i></a></li>
                            <?php } ?>

                            <?php if (get_sub_field('media_linkedin')) { ?>
                            <li class="author-profile__social-item"><a rel="noopener noreferrer" href="<?php echo get_sub_field('media_linkedin'); ?>" class="author-profile__social-anchor" target="_blank"><i class="fa fa-linkedin-square" aria-hidden="true"></i></a></li>
                            <?php } ?>

                            <?php if (get_sub_field('media_google')) { ?>
                            <li class="author-profile__social-item"><a rel="noopener noreferrer" href="<?php echo get_sub_field('media_google'); ?>" class="author-profile__social-anchor" target="_blank"><i class="fa fa-google-plus-official" aria-hidden="true"></i></a></li>
                            <?php } ?>

                            <?php if (get_sub_field('media_reddit')) { ?>
                             <li class="author-profile__social-item"><a rel="noopener noreferrer" href="<?php echo get_sub_field('media_reddit'); ?>" class="author-profile__social-anchor" target="_blank"><i class="fa fa-reddit-alien" aria-hidden="true"></i></i></a></li>
                            <?php } ?>

                            <?php if (get_sub_field('media_personal_website')) { ?>
                             <li class="author-profile__social-item"><a rel="noopener noreferrer" href="<?php echo get_sub_field('media_personal_website'); ?>" class="author-profile__social-anchor" target="_blank"><i class="fa fa-globe" aria-hidden="true"></i></a></li>
                            <?php } ?>

                        <?php endif; ?>
                  <?php  endwhile;

                    else :
                        echo 'no content';
                    endif;


                endwhile; // End of the loop.

                ?>

但是,这是什么,但它每次显示 3 次。

所以它显示了 facebook、git、linke、facebook、git、linke、facebook、git、linke

它不显示未设置值的那些。

我怎样才能不让他们重复那些已经设置好的?

ACF 灵活内容

【问题讨论】:

  • 您查看过您创建的 ACF 字段吗?检查的性质:` if (get_sub_field('media_facebook')) ` 应该完全为空才能为 FALSE。换句话说,如果您有 a = " ",a = TRUE,并且您的 if 语句将显示该字段。一种测试方法是检查字段中的内容是 var_dump("the_field('media_facebook')"),如果您有未定义以外的任何内容,即使您没有在页面上设置默认值,也会设置默认值.
  • 唯一显示的是我填写的字段,没有填写的不显示。但是,填充的值会显示几次。就像在这个截图中一样 - i.imgur.com/0pyzjVW.png

标签: wordpress advanced-custom-fields acfpro


【解决方案1】:

尝试使用PHP函数empty检查是否有值。

<?php if (!empty(get_sub_field('media_google'))) { ?>
<li class="author-profile__social-item"><a rel="noopener noreferrer" href="<?php echo esc_url( get_sub_field('media_google') ); ?>" class="author-profile__social-anchor" target="_blank"><i class="fa fa-google-plus-official" aria-hidden="true"></i></a></li>
<?php } ?>

我认为 get_sub_field 不会转义您的输出,因此您应该在显示值时使用函数esc_attrhere 编写了一个很酷的辅助函数,您可以使用它,否则只需使用原生 WordPress 函数即可。

【讨论】:

  • 嗯,我已经尝试过类似的解决方案,但是你刚刚拥有的代码也不起作用。它打破了页面。该代码对我来说看起来不错,不知道为什么它会破坏它。我只尝试了!empty,然后粘贴了整个代码,但它不起作用,它会杀死页面并且我得到死屏。
  • 检查你的 debug.log 它会告诉你错误在哪里。
猜你喜欢
  • 2020-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-06
  • 1970-01-01
  • 2019-01-04
相关资源
最近更新 更多