28 Jun 2015

Displaying specific custom fields

There are many times I need to display a spcific custom field inside a post. An example of this is where I have set up custom fields for various social networking URL's, and need to style those URL's into icons or badges. The following shows how I have done this with four custom fields.

<?php $Facebook = get_post_meta($post->ID, 'Facebook', true);
if ($Facebook) { ?>
<a class="fb" href="/<?php echo $Facebook; ?>" target="_blank">Facebook</a>
<?php } ?>

<?php $Twitter = get_post_meta($post->ID, 'Twitter', true);
if ($Twitter) { ?>
<a class="tw" href="/<?php echo $Twitter; ?>" target="_blank">Twitter</a>
<?php } ?>

<?php $Soundcloud = get_post_meta($post->ID, 'Soundcloud', true);
if ($Soundcloud) { ?>
<a class="sc" href="/<?php echo $Soundcloud; ?>" target="_blank">Soundcloud</a>
<?php } ?>

<?php $Website = get_post_meta($post->ID, 'Website', true);
if ($Website) { ?>
<a class="ws" href="/<?php echo $Website; ?>" target="_blank">Website</a>
<?php } ?>