Cornerstone: Using Custom Field Data in WordPress

By Sol in Know

Cornerstone’s Content Type functionality enable you to add additional data to a content item based on it’s post type.  This is a brief tutorial on how to use that data.

Content types?

First, you need to create a content type with custom fields.  If you haven’t already, learn more about creating custom content types and then come back and pick up where you left off.

For this tutorial, we’re going to continue to use the custom content type, book, as an example.

Our book content type has two fields:

  • Author (author) – Author’s name
  • Publisher (publisher) – Publishing Company

How it’s done

Once you’ve added a few books to your WordPress site, you’ll probably want to display the author and publisher of each book on their respective pages.  Doing this is very simple and very similar to using any of WordPress’ post template tags in a theme.

Note: As with WordPress’ post template tags, you should be in the loop when using Cornerstone’s post-specific template tags.

There are two template tags for accessing a post’s field data:

  • cnr_get_data(string $field_id)
  • cnr_the_data(string $field_id)

cnr_get_data() returns data for a specified field (e.g. for saving into a variable), and is used like so:

$author = cnr_get_data('author');
//Saves author field data $author variable

cnr_the_data() outputs (i.e. prints) data for a specified field to the page, and is used like so:

//Prints publisher name to page

The End (for now)

That’s pretty much all there is to making use of the field data in your WordPress themes.  There are other features such as mutliple layouts and output formats, so feel free to explore the code to learn more about Cornerstone’s capabilities.

Have fun!