HowTo – WordPress Using Custom Fields In Your Posts

Filed under: Wordpress Tips

wordpress_logoThere are as many reasons as you can think of to add a custom field to your posts.

You may want to

Display an image for the post on your home or category pages.

Add a link on a product page to buy the item with paypal.

Add a special coupon ID number for a product.

Display information from other sites.

So, you have your own reason but how do you add a Custom Field and then use it in your post?

First you should add the custom field to a post so you have sample info to work with.

Open one of your posts and go to the bottom of the edit window.

You will see the Custom Fields Box expand it if you need to.

On the left you will see a dropdown of current Custom Fields. Some Plugins use custom fields so you may already have a few but make your own.

Select Enter new and a Text Box will open, Enter the name and add some sample data in the Value box on the right and press Add Custom Field.

This field will be available in the dropdown menu when you edit other posts but the Value Data will be available only for the post you enter it in.

So, Lets make a Custom Field and Enter some Data in the Value Box.

Name the Field: availability
In the Value put: Yes

This custom field will be used to tell our visitors if the product shown in the post is available for purchase. If at a later time we sell out of the product we go to the post and change the value and it will update on our post page.

Lets use the Custom Field Data in our single.php Theme Template File.

First you need to find the loop that displays the specific post.
You should know where to find it if you have done any theme file editing before but for new designers look for

This is the Beginning of the loop – <?php if (have_posts())

This is the End of the Loop – <?php endif; ?>

Now you may have more then one endif in your loop so make sure it is the one that matches up with the beginning if statement In PHP if statement have a start and end just like HTML Tags <a>stuff</a> <?php if …. > Stuff <?php endif>

Lets find our Permalink Title in the Single File and put the Availability Custom Field Data Under it so our visitors will know right away if the product is available.

Look for

<?php the_title(); ?>

Add this after it

<h2>Product Available:
<?php $key=”availability”;
$onshelf = get_post_meta($post->ID, $key, TRUE);
echo “$onshelf”;
?>
</h2>

How Will it look?

Open the post that you added the custom field for in your browser. Make sure you have your supercache or other cache turned off and your browser cache refreshed. crtl-enter will clear your browser cache once you load the page.

You should see the Title of the post and then under it a line that says

Product Available: Yes

It really is that easy but remember you need to put the code within a loop and you need to call the correct key to get the data back.

So where else can you use this info?

How about on your category pages. If you want your visitors to browse your categories like a store you can show a permalink in the category and under it put the code for availability. You can also add another Custom Field to show price or any other data you want to show.

Get Creative
Use the custom field to show an image or show a rating or to highlight your posts.

The ideas are endless and up to you to find.