Currently Browsing: Custom Fields

Use a custom field outside the loop

Custom fields are so flexible and being able to display them outside the loop might certainly come in handy.

Display custom fields in your theme

This is a simple bit of code that can save you a lot of time if you know you’ll have recurring items in each post. For example, the “read article” button in most of the posts on this site is done using a custom field called “link” within a div container called “link”. Here’s the code:

 <?php if(get_post_meta($post->ID, 'link', true)): ?>
<div class="link">
<h5><a href="<?php echo get_post_meta($post->ID, 'link', true); ?>">
Read Article</a></h5>
</div>
<?php endif; ?>

The code, in plain English, says: If there is a custom field in the post called link, then display the link button with the link in the custom field, using “Read Article” as the link text.