1

I'm using ACF, flexible-content.

I have made media fields for facebook, twitter and more.

The fields will get the link, as the front-end is just the logo of the company.

I need to show the html, only if the value is set, if not, don't show it.

How can I accomplish this? I need something like this:

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

I have tried this:

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

                ?>

However, what is this, but it shows 3times each.

So it shows facebook, git,linke, facebook, git, linke, facebook, git, linke

It doesn't show those where the value isn't set.

How can I not make them repeat those who are set?

ACF Flexible Content

  • Have you looked at the ACF field you created? The nature of the check: ` if (get_sub_field('media_facebook')) ` should be completely empty to be FALSE. In other words, if you have a = " ", a = TRUE, and your if statement would show the field. A way to test is to check what is in the field is to var_dump("the_field('media_facebook')"), if you have anything other than undefined, a default value is being set even if you did not set it on the page. – Pedro Ferrari Jan 06 '18 at 22:22
  • The only thing that displays are the fields I have filled in, the one not filled, don't show. However, the filled values display few times. Like in this screenshot - https://i.imgur.com/0pyzjVW.png – Aurelian Spodarec Jan 06 '18 at 22:30

1 Answers1

0

Try the PHP function empty to check if there is a value.

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

I don't think get_sub_field escapes your output so you should employ the function esc_attr when displaying the values. There's a cool helper function written here that you could use otherwise just use the native WordPress functions.

Andrew Schultz
  • 4,092
  • 2
  • 21
  • 44
  • Hmmm, I have tried solutions similar to that one, however the code you just have me, doesn't work either. It breaks the page. The code looks good to me, not sure why it breaks it. I tried with just !empty, and then pasted the whole code, but it doesn't work, it kills the page and I get death screen. – Aurelian Spodarec Jan 07 '18 at 01:11
  • Check your debug.log it will tell you where the error is. – Andrew Schultz Jan 07 '18 at 01:26