Skip to content

Instantly share code, notes, and snippets.

@morgyface
Last active April 11, 2024 22:37
Show Gist options
  • Save morgyface/d8c1c4246843bf0f0c76959b68faa95f to your computer and use it in GitHub Desktop.
Save morgyface/d8c1c4246843bf0f0c76959b68faa95f to your computer and use it in GitHub Desktop.
WordPress | ACF | Social Media Links using Advanced Custom Fields and FontAwesome
<?php
if( have_rows('social_media', 'option') ):
echo '<div class="container social my-4">';
echo '<p class="follow mb-0 align-middle">Follow us</p>';
echo '<ul class="nav align-middle">';
while ( have_rows('social_media', 'option') ) : the_row();
$socialchannel = get_sub_field('social_channel', 'option');
$socialurl = get_sub_field('social_url', 'option');
echo '<li class="nav-item">';
echo '<a class="nav-link" rel="nofollow noopener noreferrer" href="' . $socialurl . '" target="_blank">';
echo '<i class="fa fa-' . $socialchannel . '" aria-hidden="true"></i>';
echo '<span class="sr-only hidden">' . ucfirst($socialchannel) . '</span>';
echo '</a></li>';
endwhile;
echo '</ul>';
echo '</div>';
endif;
?>
@morgyface
Copy link
Author

morgyface commented Sep 7, 2017

Also note span.hidden is probably better not to be set to display:none as it renders it completely invisible to screen readers, instead go for this:

span.hidden {
position: absolute;
overflow: hidden;
clip: rect(0 0 0 0);
height: 1px; width: 1px;
margin: -1px; padding: 0; border: 0;
}

@DecoGrafics
Copy link

DecoGrafics commented Mar 22, 2018

Some more info on how to install this would be great like what type of fields each should be... :)

@morgyface
Copy link
Author

Hey @DecoGrafics, sorry it was a bit vague, I've updated the first comment to include more detail. I hope it helps.

@shanksmax
Copy link

Hi, where to save social.php file? inside child-theme? in root?
Sorry I am a noob

@edulecca
Copy link

edulecca commented Oct 2, 2019

appreciate it!

@theSalafee
Copy link

theSalafee commented Jul 14, 2022

Great work! Awesome write up as well! I did something like this today on the job using your example. However, I used fa-youtube-play instead.

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

                    echo '<div class="social-block">';

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

                        $socialchannel = get_sub_field('social_channel', 'option');
                        $socialurl = get_sub_field('social_url', 'option');

                        echo '<a rel="noopener" target="_blank" href="' . $socialurl . '"><i
                                class="fa fa-' . $socialchannel . '" aria-hidden="true"></i><i class="fa fa-' . $socialchannel . ' two"
                                                                                aria-hidden="true"></i></a>';
                    endwhile;
                endif;
                ?>

@morgyface
Copy link
Author

Nice work @theSalafee. Glad it helped!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment