Using custom fields to display social icons
-
Hi, i’m very new to php and wordpress development so
on my site in development each ‘post’ is a review of a different website, on each ‘post’ there is an ‘about’ and then i want to have a section with the websites social profiles etc, i want to show
the icon of there social page only if they have one and then nothing if they don’t, each post will have unique profile links so i guessed that customfields would be my best bet and using php to display them or not, after a bit of researching on how to do this with custom fields i came up with the following code which works a treat:<?php if(get_post_meta($post->ID, 'facebook', true) || get_post_meta($post->ID, 'twitter', true) || get_post_meta($post->ID, 'youtube', true) ): ?> <ul> <?php if(get_post_meta($post->ID, 'facebook', true)): ?> <li><a href="<?php echo get_post_meta($post->ID, 'facebook', true); ?>"> <img src="<?php bloginfo('stylesheet_directory'); ?>/icons/facebook-icon.jpg" alt="Facebook page" /></a></li> <?php endif; ?> <?php if(get_post_meta($post->ID, 'twitter', true)): ?> <li><a href="<?php echo get_post_meta($post->ID, 'twitter', true); ?>"> <img src="<?php bloginfo('stylesheet_directory'); ?>/icons/twitter-icon.jpg" alt="Twitter page" /></a></li> <?php endif; ?> <?php if(get_post_meta($post->ID, 'youtube', true)): ?> <li><a href="<?php echo get_post_meta($post->ID, 'youtube', true); ?>"> <img src="<?php bloginfo('stylesheet_directory'); ?>/icons/youtube-icon.jpg" alt="Youtube profile" /></a></li> <?php endif; ?> </ul> <?php endif; ?>It will be alot of code once i get all the social profiles included so i’m wondering is there an easier way to do it?
Thanks
Daniel
The topic ‘Using custom fields to display social icons’ is closed to new replies.