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)
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.
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
Michael, I know it’s been ages, but I finally got a chance to piece this together: http://davidchu.net/blog/2012/03/shortcodes-genesis-custom-field/
Enjoy!
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?
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
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.
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.
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?