ACF Theme option Social Media Links with a foreach loop

<div id="sociallinks">
   <ul>	
     <?php 
	 	// Get the fields
		$facebook = get_field('facebook_link','option');
		$twitter = get_field('twitter_link','option');
		$youtube = get_field('youtube_link','option');
		$instagram = get_field('instagram_link','option');
		// Create the array	
		$facebookli = array('class'=>'facebook', 'link'=> $facebook, 'text'=>'Like us on Facebook');
		$twitterli = array('class'=>'twitter', 'link'=> $twitter, 'text'=>'Follow us on Twitter');
		$youtubeli = array('class'=>'youtube', 'link'=> $youtube, 'text'=>'Watch us on YouTube');
		$instagramli = array('class'=>'instagram', 'link'=> $instagram, 'text'=>'Follow us on Instagram');
		// If Field is not empty, add it to the array	
		$sociallinks = array();
			if($facebook != '') {
				$sociallinks[] = $facebookli;
			}
			if($twitter != '') {
				$sociallinks[] = $twitterli;
			}
			if($youtube != '') {
				$sociallinks[] = $youtubeli;
			}
			if($instagram != '') {
				$sociallinks[] = $instagramli;
			}
			?>
            
         <?php 
		 	// Run your loop to get the links
			foreach($sociallinks as $social ) { ?>
             <li class="<?php echo $social['class']; ?>">
                 <a href="<?php echo $social['link']; ?>"><?php echo $social['text']; ?></a>
             </li>
          <?php } ?>
                
    </ul>
</div><!-- sociallinks -->

 

Leave a Reply

Your email address will not be published. Required fields are marked *

− 1 = 2