如果设置了值,如何显示ACF子字段,如果没有,隐藏?

时间:2018-01-06 21:43:32

标签: wordpress advanced-custom-fields acfpro

我正在使用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灵活内容

1 个答案:

答案 0 :(得分:0)

尝试使用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_attr。有一个很酷的帮助函数here,你可以使用,否则只需使用原生的WordPress函数。