开发者

How do I return values from custom created meta box?

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?


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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜