Using a Wordpress Custom Field With the Genesis Framework

Note: this is intended for WordPress developers.

For some time now I’ve been really enjoying the excellent StudioPress Genesis Theme Framework. (In fact, I like it enough that I also became an affiliate) Developing WordPress themes is easier and more organized than ever. While learning the framework, I made use of their helpful hook reference, which offers easy access to Genesis features.

While working with a new theme, I wanted to include a custom field that would attach to a Post (or Page). Then this field would be displayed in the sidebar when viewing that Post – essentially showing some unique content for the Post that would not be displayed in the Post’s body.

I looked through the Genesis hook reference for something relating to custom fields, and I was surprised not to find one, given that there are helpful hooks for almost any purpose you can think of. After searching further through forum posts and tutorial articles, I found what I was looking for: the hook is (surprise!) genesis_custom_field.

So say you want to be able to display a slogan (or some other bit of user-entered content) displayed just above your sidebar content. Edit a Post, and look near the bottom of the page. You should see an area for Custom Fields. (If not, go to the upper right, click Screen Options, and be sure that Custom Fields is checked). Click Enter New, put in the Name and Value, and click Add Custom Field. In my case, I named the field “slogan”.

Then under Appearance… Edit, I added this code to my Theme Functions script (functions.php):

add_action( 'genesis_before_sidebar_widget_area', 'slogan_meta' );

function slogan_meta() {
  genesis_custom_field('slogan');
}

The names of things make it more-or-less self-explanatory. Essentially, I set up a function that will place an item at the top of the sidebar widget. This function consists of our new field, “slogan”. Then when you view a Post on your site, its slogan will appear at the top of the sidebar. (or you can choose not to have any slogan value for a particular Post, so none will display). That’s the simplest implementation of the custom field; you could add additional HTML code to the field for styling purposes, but this illustrates the general idea.

You can, to be sure, use regular WordPress functions and code to implement Custom Fields, but Genesis allows you to do this sort of task more elegantly, with very little code. I hope you found this helpful. Let me know in the Comments if you have a question.

Brief Commercial Message

Looking for a very clean, modern WordPress theme that’s also responsive? (one that will look great on different devices from mobile to desktop) Have a look at Executive! Despite the name, it’s not “too” corporate, and would work for many types of sites. (I am an affiliate, and a wholehearted proponent of Genesis themes)

21 Comments


  1. This worked great, and it was super easy for a non php guy like me.

    Any idea of how you could get Custom Fields to Parse Shortcode? or even just PHP?
    I’m trying to drop something via ShortCode into the genesis_before_content_sidebar_wrap area. But the all it does it place the shortcode there.

    Reply

    1. Hi Michael,
      Glad that worked for you. I have never tried using shortcodes in a custom field. Doing that or using PHP there sounds pretty tricky, even maybe a security risk. Here’s a search that may help you.
      I have heard of plugins that allow PHP into unusual places, so that might be worth investigating.
      Cheers, Dave

      Reply

  2. Hi dave,

    I’m new to wordpress & genesis, It would had been a great tutorial if you had added screenshots.

    I had a scenario , may be you can help me with this.

    I have 3 custom fields.I want to display one custom field after one paragraph & other after another paragraph.

    Is there any chance?

    Reply

  3. Chris,
    That’s true, and if I were writing for newbies as opposed to for developers, I may have gone that far.

    In any case, my designer colleague asked a similar question. The short answer is: not really. If you’re talking about regular page content, the loop puts all of the content out in one big blob – you’d have to write some pretty wild PHP to analyze the content, parsing it to find paragraphs, and stick the custom fields in between them or something. Not my idea of fun.

    If I were going to try something like that, I would set up custom fields and also shortcodes for them. Then the shortcodes could be stuck wherever in the content. You could also make some sort of custom loop. But all that is well beyond the scope of this article, and really for more experienced developers.

    Here’s a random thought for you. You could make custom fields for your paragraphs, too. Then you just pump them out in order: paragraph… field…. paragraph… field…, and so on, into your template. Then you just don’t bother entering regular page content.

    Good luck, D

    Reply

  4. I’d just like to add that genesis_get_custom_field() will return the custom field value while genesis_custom_field() will echo the value.

    Reply

    1. Great point, Josh!

      That brings up another point. Genesis is, frankly, a little light on online documentation, though it is improving. As a comparison, Thesis has more! 🙂 So if you’re intending to be a serious Genesis developer, I think that at some point you’ll need to read code to see all the fancy little functions that aren’t listed online. I know, I was reluctant myself, but when I really need something deep or complex that’s not found here, I now scan the codebase with my favorite text scanner using related search terms, invariably find new riches, and nearly always find a way to do what I want to do.

      I know this may be unwelcome news for someone who is hoping to do really fancy stuff without touching or reading PHP code. With any of these WP frameworks, you reach a point where you can’t “framework” your way around a problem.

      Another interesting finding for me is that when I can’t readily find an answer using Genesis hooks, I nearly always can do it another way with stock WP functions and hooks – there are zillions of very nice ones.

      Reply

  5. Hello,

    Thank you for making a nice and simple tutorial that I can actually follow 🙂 I’m trying to use a custom template page for several page but when I try you’re code it displays the actual “value” on the page. So when I did “add custom field”, I put blabla as the name and 27 as the value, added your code to that template and all that displays on the page now is “27”. Do you know what i’m doing wrong or what I can try?

    Reply

  6. Hi Tania (I’ve been away),
    Yes, my code will display the value of the custom field. A simple example of using this would be to stick a bit of custom text in a sidebar or in content.

    Maybe if I know exactly what you’re trying to display on your page, I can make a suggestion.

    Dave

    Reply

  7. Worked like a charm, but do you if I should be able to use the genesis_after_comments while using disqus? Can’t get anything to display after my disqus thread.

    Reply

    1. Daniel,
      Good question. I don’t know Disqus, but I would bet that it could be replacing the normal WordPress comment area and hooks, which presumably knocks out the Genesis comment hooks. I’d suggest a hook further down, but I’m guessing that this would not be where you want it, very close to each comment.

      3rd party stuff is usually tricky to modify. My last thought is that if the Disqus plugin has any handy hooks, that might be a way to do that. But that’s a long shot. Sorry!

      Dave

      Reply

  8. I can’t get this to work but being a newbie at using custom fields I might have missed a big step. I created a custom field “feature-text’ and added this to the functions.php but nothing is showing. I created the custom field using a plugin called wp-types.


    //* add feature content to home page
    add_action( 'genesis_before_content', 'homepage_feature' );
    function homepage_feature() {
    genesis_custom_field('feature-text');
    }

    Reply

    1. Hi,
      I also use Types and enjoy it very much. There’s a trick to using it if you created the field in Types… if you don’t use Views, and want to display it the regular WordPress way, just add “wpcf-” as the prefix to your custom field name.

      Dave

      Reply

  9. Dave,

    How may I use a custom field to add contents (text or image) to a text box inside a sidebar widget?

    Thank-you.

    Mike

    Reply

    1. Mike,
      Good question. Since PHP won’t go into a text widget, I would set up a shortcode which would “represent” the contents of the custom field, and I might include some HTML in the text widget with a class or two to style the whole thing via CSS. Sorry, not a simple answer – programming and geekery needed. I may write a tutorial on that if time allows.

      Dave

      Reply

      1. Dave,

        I managed to find a tutorial online, which I plan to try out this weekend.

        I think I can use a short code in each post or page.

        My next question is…how do I insert a short code into a homepage?

        Ultimately, I want to have a unique content in the sidebar widget textbox in the homepage, post and page.

        Thank-you.

        Mike

        Reply

        1. Mike,
          There’s no single answer. It could be in page content, it could be in theme template files, it could be in some theme setting, on and on.

          Good luck.

          Reply

  10. This is great! I wasn’t sure how to do this and after almost an hour of searching online I found the answer here.

    How do you add HTML to the output, though? I’d like to add a label to the front to the output (e.g., Time) and I’d like to add a CSS class but it is not working. Any help would be much appreciated!

    Thanks,
    Mike

    Reply

    1. Mike, You’ll need to know at least a little PHP to do that. In that situation, you’d probably be using some ECHO statements to display some HTML.

      Dave

      Reply

  11. This was fabulously useful! Thank you for taking the time to post it – you saved me HOURS trying to figure out how to get these custom fields to show up. I made a few changes to your code and they now appear at the top of my posts. I am not that comfortable with PHP but did manage to add an ECHO for each label. Looks great.

    Double thumbs up, Tech Tabby.

    Reply

    1. Mary, That’s great, I’m glad to hear that it helped you!

      Dave

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *