doushadu0901 2010-04-28 07:04
浏览 15
已采纳

如何从自定义创建的元框返回值?

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条回答 默认 最新

  • doudengjin8251 2010-04-29 07:41
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器