From the Blogabout me

Displaying Custom Fields for Genesis

If you’ve ever worked with Genesis framework for WordPress, or even Gravity Forms, you will appreciate this quick tutorial.

Gravity Forms has a great feature that allows for you to create a form that users can create Posts, but if you have them add to a Custom Field, it doesn’t display automatically, so you have to figure that out on your own.

I’ll start from the Genesis point of view, because I want to.

I want to display all of my Custom Fields on my Posts, what do I do?

Step #1 = create your Gravity Form to add to your Custom Fields
Step #2 = users add new entries to the form, and it is added to your WordPress Posts area
Step #3 = Add the code to your functions.php file

More details here though:

Step #1 =

First you have to look at your Gravity Forms form.  For my example, I am going to use a Directory style form with fairly common fields.

Phone Number:
Email Address:
Position/Title:

So the form setup looks something like this (I know the image is too hard to read, so click on it to see full size):

custom-fields

 

As you can see I either added a Custom Field Post type to an existing Custom Field that was already created before I started the form (title, email_addy) but I wanted to create a new Custom Field on the fly (position) so I clicked on New and typed in the field name I wanted to use.  Notice, you can also select what type of Field Type you want the Custom Field to be through Gravity Forms, which is pretty darn awesome.

Step #2 =

So when a visitor goes to submit information they see something like this with the form:

custom-form

And when they submit the info, it will look something like this on their Post via the Dashboard of WordPress

post-customfields

With me so far?  If not, please go through the steps again before Step #3…

Step #3 =

Adding the code to your functions.php file can look scary, but I promise it isn’t too bad.

Okay, now open your theme’s functions.php file, and using this generic code, make changes to it as you see fit:

/* Custom Fields
*/ by: kevin shoffner
*/
add_action('genesis_post_content', 'field-name');
function field-name() {
if ( is_single() && genesis_get_custom_field('field-name') )
echo '<hr /><div id="field-name">Title: '. genesis_get_custom_field('field-name') .'</div>';
}


But what if I want some special formatting or ordering of my custom fields?

In my case, I had some extra formatting and customizations, but you can see what I did using the same Custom Fields from above:

add_action('genesis_post_content', 'title');
function title() {
if ( is_single() && genesis_get_custom_field('title') )
echo '<hr /><div id="postition_title">Title: '. genesis_get_custom_field('title') .'</div>';
}

add_action('genesis_post_content', 'phone');
function phone() {
if ( is_single() && genesis_get_custom_field('phone') )
echo '<div id="phone">Phone Number: '. '<a href="tel:' .genesis_get_custom_field('phone') . '">' . genesis_get_custom_field('phone') .'</a></div>';
}

add_action('genesis_post_content', 'email_addy');
function email_addy() {
if ( is_single() && genesis_get_custom_field('email_addy') )
echo '<div id="email_addy">Email Address: '. '<a href="mailto:' . genesis_get_custom_field('email_addy') . '">' . genesis_get_custom_field('email_addy') .'</a></div>';
}

add_action('genesis_post_content', 'website_url');
function website_url() {
if ( is_single() && genesis_get_custom_field('website_url') )
echo '<div id="website_url">Website: ' . '<a href="' . genesis_get_custom_field('website_url') .'">' . genesis_get_custom_field('website_url') . '</a></div>';
}

This now displays something like

displayed-customfields

 

I highly recommend Gravity Forms by Rocket Genius if you currently are not using it with your WordPress site.

Comments

  1. Sally Minker says:

    Thank you so much for this code. I have been trying to display a field in a Genesis Theme and using your code helped me do it! Yes!!!

Trackbacks

  1. […] Great news, I made a tutorial for that very reason.  Check it out! […]

Leave a Reply

%d bloggers like this: