Community Forums › Forums › Archived Forums › Design Tips and Tricks › Making a custom field appear in a post
- This topic has 8 replies, 3 voices, and was last updated 11 years, 2 months ago by webmonkey.
-
AuthorPosts
-
January 5, 2013 at 6:48 pm #9820webmonkeyMember
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
Condition: Good
Price: $500I 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[0].", "; ?>
<?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 #9824AnitaKeymasterWhen 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.
Love coffee, chocolate and my Bella!
January 5, 2013 at 7:38 pm #9829webmonkeyMemberThanks 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 > etc
January 5, 2013 at 7:48 pm #9830AnitaKeymasterIf you install and use the Genesis Simple Hooks, you can place the following code in the appropriate Hook area. Here is the Hook Guide.
<!--?php the_meta(); ?-->
Love coffee, chocolate and my Bella!
January 5, 2013 at 7:52 pm #9832AnitaKeymasterI 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.
Love coffee, chocolate and my Bella!
January 5, 2013 at 10:19 pm #9874Bill MurrayMemberSee my reply in this thread.
Web: https://wpperform.com or Twitter: @wpperform
We do managed WordPress hosting.
January 8, 2013 at 3:02 pm #10448webmonkeyMemberThanks Bill.
You say:
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?
<?php
$custom-field1 = genesis_get_custom_field ('custom-field1');
echo "Label: " .$custom-field1[0]. ;
?>January 8, 2013 at 3:08 pm #10451Bill MurrayMemberYou need this line in that block of code:
global $post;
It also looks like you have an extra period after your closing bracket.
Web: https://wpperform.com or Twitter: @wpperform
We do managed WordPress hosting.
January 8, 2013 at 4:23 pm #10474webmonkeyMemberThanks Bill, bit of solution crossover here. Here's what ended up working:
1. <?php
2. if( genesis_get_custom_field( 'CUSTOMFIELDNAME' ))
3. echo 'Artist: ' .genesis_get_custom_field( 'CUSTOMFIELDNAME' ).;
4. ?>This returns
Artist: CUSTOMFIELDVALUE
on a pageIn 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.
http://designsbynickthegeek.com/tutorials/how-i-make-custom-fields-easier
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.