最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

post meta - Adding Custom MetaData

programmeradmin2浏览0评论

In Professional Wordpress Design & Development the author states that WordPress features a simple function to add new post metadata called add_post_meta().

This function will attach a piece of metadata to the post as follows:

   <?php  add_post_meta ( $post_id, $meta_key, $meta_value, $unique ); ?>

I've tried adding this code to my functions.php file without the < ? php ? > , and nothing happens. Where would this code go to successfully add a custom metadata box to a post?

In Professional Wordpress Design & Development the author states that WordPress features a simple function to add new post metadata called add_post_meta().

This function will attach a piece of metadata to the post as follows:

   <?php  add_post_meta ( $post_id, $meta_key, $meta_value, $unique ); ?>

I've tried adding this code to my functions.php file without the < ? php ? > , and nothing happens. Where would this code go to successfully add a custom metadata box to a post?

Share Improve this question asked Jun 7, 2019 at 23:17 WestWest 211 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

that function is explained here and if I understand it right, it's not quite THAT simple. To use it as a single line in functions, you would have to fill out all those variables.

 add_post_meta ( 7, 'fruit', 'banana' )

This would literally output only on the admin of Post ID 7, and it would create a field called "fruit" with the value of "banana".

So it's simple...in the fact that it can be used in a more complex function to create post meta boxes. You would need to run this in a loop to get it to attach to every post, and depending on how robust you want your fields to be. I'm also not sure if it saves (it looks to me like you need to write that functionality in).

If you're looking to add custom meta boxes to your posts, I HIGHLY recommend using Advanced Custom Fields. You don't need Pro to use it, though it's 100% worth the purchase. It will easily assign to all posts of a type, or to individual posts and pages, it saves when edited, and it's got great documentation for how to call the data into your templates.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论