January 5, 2013 at 6:48 pm #9820
I’ve literally spent days on what should be a simple task.
I have a bunch of custom field values that I’d like to display, in an order I select, conditionally (if present), under the title of a post, eg.
Title of WordPress Post
Artist: John Smith
Year created: 2006
I stuck the following code in Genesis Simple Hooks’ genesis_after_post_title Hook and set it to Execute PHP:
<?php echo “<div>”; ?>
<?php $gfmartist = get_post_custom_values(“gfm-artist”); ?>
<?php if ($gfmartist!=””) echo “Artist “.$gfmartist.”, “; ?>
<?php echo “</div>”; ?>
Nothing shows up.
I have tried this on Prose and News child themes. I have searched WordPress Codex perhaps the most impenetrable “help” documents ever written for man or beast, I have read multiple custom field tutorials found on Google (including Studiopress’ Nick the Greek’s one).
I don’t get it. Presumably this is as simple as adding the code above, and I’m just a little off somewhere?January 5, 2013 at 7:00 pm #9824
When I used custom fields, I followed these instructions. Not sure if this will help you at all, but check it out – http://codex.wordpress.org/Custom_Fields.
January 5, 2013 at 7:38 pm #9829
Thanks anitac, but that was the impenetrable WordPress Codex page I was talking about. I’ve worked down that page trying all the various codes in various permutations with no luck at all.
WordPress’ Codex has some ridiculous blindspots, probably because the codex is written by programmers and not technical writers.
Notice that on this specific page, there’s no simple copy and paste code snippet in the entire page that returns a single custom field when placed in a template. The page instead starts with a minimalist post_meta command—which does nothing as is that I can see—and then skips to advanced and legacy uses, assuming a bunch of knowledge that isn’t linked.
My favorite part on that page is:
<?php echo get_post_meta($post->ID, 'key', true); ?>
will echo the value for a single key as a string wherever it is used in a template file.
No it doesn't.
<?php $key="mykey"; echo get_post_meta($post->ID, $key, true); ?>also fails even though gfm-artist is the custom field name.
I've tried a bunch of stuff like
<?php $test = get_post_meta($post->ID, 'gfm-artist', TRUE); ?>
<?php if($test!="") echo $test; ?>
Nothing doing. This is the kind of stuff in other CMSes that is dealt with by adding a shortcode like "$gfm-artist$" to a template and you're done. I can't believe it's this difficult and this badly documented.
Literally days of trying to get this ridiculously simple thing working. I am amazed at the Google fail on this simple issue. Help on the first few pages of Google searches for "display custom field wordpress" is either for legacy WP versions or similarly impenetrably written.
And hey, StudioPress: the CODE HTML tag in this forum gets confused if you post code with tags. LT and GT angle brackets break the posts, unless you put > etcJanuary 5, 2013 at 7:48 pm #9830
<!--?php the_meta(); ?-->
January 5, 2013 at 7:52 pm #9832
I see you said above you had installed Simple Hooks already. You are trying to call the Meta I believe but that’s not in your code anywhere. I am just sharing how I got mine to work.
January 5, 2013 at 10:19 pm #9874
Bill MurrayParticipantPost count: 572January 8, 2013 at 3:02 pm #10448
Make $post global and use the function genesis_get_custom_field(), as in genesis_get_custom_field( ‘myfield’ )
I didn’t understand the “Make $post global” part. Assuming a custom field is called custom-field1, and I want the page to return:
Label: [custom-field1 value]
…does this look about right?
$custom-field1 = genesis_get_custom_field (‘custom-field1′);
echo “Label: ” .$custom-field1. ;
?>January 8, 2013 at 3:08 pm #10451
Bill MurrayParticipantPost count: 572January 8, 2013 at 4:23 pm #10474
Thanks Bill, bit of solution crossover here. Here’s what ended up working:
2. if( genesis_get_custom_field( ‘CUSTOMFIELDNAME’ ))
3. echo ‘Artist: ‘ .genesis_get_custom_field( ‘CUSTOMFIELDNAME’ ).;
on a page
In the Nick the Greek tutorial, where this code was taken from, please note that the second close bracket on line 2 above is missing. The above code is correct.
You must be logged in to reply to this topic.