---
title: "WordPress Tip: Adding Custom Fields in Posts"
date: 2010-09-11
description: "$( document ).ready(function() { // Handler for .ready() called. $('#toc').toc({ minimumHeaders: 3, listType: 'ul', showSpeed: 0, headers: '#content h2, #content h3, #content h4, #content h5, #content h6' }); /*..."
canonical_url: https://idratherbewriting.com/2010/09/11/wordpress-tip-adding-custom-fields-in-posts/index.html
---

> For AI agents: a documentation index is available at https://idratherbewriting.com/llms.txt. Markdown versions of all pages are available by appending .md to any page URL.

# WordPress Tip: Adding Custom Fields in Posts

You can add custom fields below posts, and then pull out the values of those custom fields and insert them into a template. This reduces the sophistication behind styling post data, and automates the display in a template. This technique relies on the [Custom Fields Template plugin.](http://wordpress.org/extend/plugins/custom-field-template/)https://www.youtube.com/watch?v=L3VXnryN9iY Here are the main steps:

1. Install the [Custom Field Template](http://wordpress.org/extend/plugins/custom-field-template).

2. Navigate to Appearance > Editor and include this code snippet in your functions.php file:

```
function getCustomField($theField) {
global $post;
$block = get_post_meta($post-&gt;ID, $theField);
if($block){
foreach(($block) as $blocks) {
echo $blocks;
}
}
}
```

3. Configure the custom fields by going to Settings > Custom Field Template, expand the template content section, and add something like this:

```
[Day Number]
type = text
size = 10
output = true
```

Note: Unless you add output = true, the custom fields won't work.

4. Go to Appearance > Editor and add this code into the template (such as single.php) where you want the value of the custom field to appear:

```
<?php getCustomField('Total Miles to Date'); ?>
```

5. To add a conditional statement around the getCustomField function (so that it only appears under certain conditions, such as the post being in a specific category), include this before the getCustomField function:

```
<?php if (in_category('3')){ ?>
```

... then insert your getCustomField functions ...

and then close with this:

```
<!--?php } else { ?-->   <!--?php } ?-->
```

Here's [more information about conditional tags](http://codex.wordpress.org/Conditional_Tags) in WordPress.
