0

I am trying to use ACF Pro Select field to display a specify social media icons. So I have this code in my theme:

<?php if( get_field('social_medias', 'option') == 'facebook' ): ?>
           <li>
                <a href="#">
                    <svg viewbox="0 0 6.5 14" xmlns="http://www.w3.org/2000/svg">
                    <path class="st0" d="M1.4 14h2.9v-7h2l.2-2.5h-2.2v-1.4c0-.3.2-.6.5-.7h1.7000000000000002v-2.4h-2.2c-1.5-.1-2.8 1-2.9 2.5v2h-1.4v2.5h1.4v7z" id="Layer_3"></path></svg>
                </a>
            </li>
        <?php endif; ?>

"social_medias" is the SELECT field with several options, like Facebook or Twitter. What's wrong with this code? It's doesn't seems to works :D

closed as off-topic by Jack Johansson, fuxia Aug 2 '17 at 11:57

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

2

You can loop through your ACF options by using the code below.

<?php if( have_rows('social_medias', 'option') ):

    while ( have_rows('social_medias', 'option') ) : the_row(); 

      $type = get_sub_field('type'); ?>


          <?php if ($type == "facebook") { ?>

                <li>
                     <a href="#">
                             <svg viewbox="0 0 6.5 14" xmlns="http://www.w3.org/2000/svg">
                             <path class="st0" d="M1.4 14h2.9v-7h2l.2-2.5h-2.2v-1.4c0-.3.2-.6.5-.7h1.7000000000000002v-2.4h-2.2c-1.5-.1-2.8 1-2.9 2.5v2h-1.4v2.5h1.4v7z" id="Layer_3"></path></svg>
                     </a>
              </li>

          <?php } elseif ($type == "twitter") { ?>

                <li>
                     <a href="#">
                             <svg viewbox="0 0 6.5 14" xmlns="http://www.w3.org/2000/svg">
                             <path class="st0" d="M1.4 14h2.9v-7h2l.2-2.5h-2.2v-1.4c0-.3.2-.6.5-.7h1.7000000000000002v-2.4h-2.2c-1.5-.1-2.8 1-2.9 2.5v2h-1.4v2.5h1.4v7z" id="Layer_3"></path></svg>
                     </a>
                </li>

            <?php } elseif ($type == "google") { ?>

                <li>
                     <a href="#">
                             <svg viewbox="0 0 6.5 14" xmlns="http://www.w3.org/2000/svg">
                             <path class="st0" d="M1.4 14h2.9v-7h2l.2-2.5h-2.2v-1.4c0-.3.2-.6.5-.7h1.7000000000000002v-2.4h-2.2c-1.5-.1-2.8 1-2.9 2.5v2h-1.4v2.5h1.4v7z" id="Layer_3"></path></svg>
                     </a>
                </li>


<?php   } endwhile; endif; ?>
1

I would do this instead:

    <?php
    if( have_rows('social_medias', 'option') ):
        while ( have_rows('social_medias', 'option') ) : the_row();
            $type = get_sub_field('type');
            if ($type == "facebook") {

            } elseif ($type == "twitter") {

            }
        endwhile;
    endif;
    ?>
0

There could be several things wrong here. I'll run through trouble shooting.

Remember that when you're putting in your information for the select options that "facebook" doesn't equal "Facebook". so this

<?php if( get_field('social_medias', 'option') == 'facebook' ): ?>

may need to be

<?php if( get_field('social_medias', 'option') == 'Facebook' ): ?>

If you're not using values and labels, make sure you have the correct option chosen in the field options page to only return values. if you have values and labels, and you're returning both then maybe this code will work:

<?php if( get_field('social_medias', 'option')['value'] == 'facebook' ): ?>

again making sure that you're aware of capital or lowercase.

Not the answer you're looking for? Browse other questions tagged or ask your own question.