tags:

views:

96

answers:

2

I've just followed this example from Wordpress and I have successfully added an extra Meta Box in Post interface, and the value is stored in DB.

Now my question is, how can I retrieve and display the content of this meta box?

I'm trying the following code:

  $intro = get_post_meta($post->ID, 'post_intro', true);
  echo $intro;

But I get nada. What am I doing wrong?

And while I'm here, does anybody know if I can place this extra meta box above the default text box in Wordpress post page?

+2  A: 

is your snippet within the loop? If so use get_the_ID() instead of $post->ID.

it should look the this:

$intro = get_post_meta(get_the_ID(), 'post_intro', true);
echo $intro;

If you need to get you meta data outside the loop do this:

global $post;
$intro = get_post_meta($post->ID, 'post_intro', true);
echo $intro;

The reason you were getting nothing is because you have to globalize the $post variable if you want to access it. Always use the first method unless you have no choice. If your trying you use meta data for page templates please say so because I have a better solution for handling meta data in that situation.

Good luck!

Robert Hurst
RobertWHurst is spot on. As for adding the extra meta box above the default visual editor you may be able to do that using a hook. Reference Adam Brown's WordPress hook/filter list: http://adambrown.info/p/wp_hooks/
hsatterwhite
Thanks Robert. Yes, I'm using it in a page template. One article list, and in the normal page.php and single.php.
Steven
@Steven Actually I meant custom page templates, sorry for not being clear. the method above will work just fine in page.php and single.php as these are standard template files. If you're curious about what I mean check out this article at CSS Tricks by Chris Coyer.http://css-tricks.com/video-screencasts/41-wordpress-as-a-cms/
Robert Hurst
In additions to @RobertWHurst's CSS-Tricks link check out the article Smashing Magazine published today: http://www.smashingmagazine.com/2010/04/29/extend-wordpress-with-custom-fields/
hsatterwhite
This is great guys. Thanks. It's really good to be one step closer to a full fledge CMS.If you guys by any chance know how to relate files (image / document) to a post, please let me know :) Currently I manually have to paste the URL for the file into a text box. It would be sweet if I could select the file from a dialog box.
Steven
A: 

As a side note I'd like to reference a WordPress Meta Box PHP Helper class that might help you and others when working with WordPress Meta Boxes.

farinspace