高级动态 WordPress 自定义元框
我正在创建一个插件,该插件创建多个自定义帖子类型(CPT)(所有这些都工作得很好)。其中一些 CPT 需要一个“自定义写入面板”,这基本上意味着它有一组自定义元框,而不是您在帖子或页面中获得的标准“编辑”框。
其中一个 CPT 需要一组“动态”元框,这些元框基本上具有不确定数量的值。我会尝试举一个例子:(
这不是我正在做的事情,但这似乎是我希望它如何工作的一个很好的例子)
想象一下我们有一个“房屋”的 CPT 和其中一个此 CPT 的自定义元选项是“卧室” - 显然,不同的房屋有不同数量的卧室,所有卧室都有不同的属性。因此,我希望能够允许用户添加有关卧室的多个不同属性,即
尺寸、方面、窗户数量和地板类型
当用户为第一间卧室添加这些详细信息时,如果房子有第二间卧室卧室,然后我希望他们有一个“添加卧室”按钮,并添加另一组这些元框,以便他们可以填写有关卧室 2 的详细信息。
我知道我需要一些 javascript 来添加此设施(以及一些东西)我不介意写)但是我可怜的、无用的大脑正在努力解决的是如何将这些保存到数据库中。我想使用 WordPress 元来实现这一点,这样我就不会添加任何不必要的数据库表,而且我确信我可能不太明白如何保存数据的原理。
任何教程的指针都会很棒,或者一些已经完成此类操作的插件示例将非常感激。
I'm in the process of creating a plugin which creates several custom post types (CPT) (all of which work just fine). Some of these CPTs require a 'custom write panel' which basically means that it has a set of custom meta boxes rather than the bog-standard 'edit' box you get with your posts or pages.
One of these CPTs requires a 'dynamic' set of meta boxes which basically has an indeterminate amount of values. I'll try and give an example:
(this isn't what I'm doing, but it seems like a decent example of how I'd like it to work)
Imagine we have a CPT of 'houses' and one of the custom meta options for this CPT is 'bedrooms' - obviously, different houses have different amount of bedrooms, all of which have different properties. So, I'd like to be able to allow the user to add several different properties about a bedroom i.e.
Dimensions, aspect, number of windows and floor type
When the user has added these details for the first bedroom, if the house has a second bedroom then I would like them to have an 'add bedroom' button and another set of these meta boxes is added so they can fill out the details about bedroom 2.
I understand that I'll need some javascript to add this facility (and something I don't mind writing) but what I'm struggling with in my poor, useless brain is how to save these to the database. I want to achieve this using WordPress meta so that I don't add any unnecessary database tables and I'm sure it's possible I just can't quite fathom the principles of how to save the data.
Pointers to any tutorials would be brilliant, or some examples of plugins which already do this kind of thing would be very gratefully received.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是关于创建元框的非常好的文章。
然后,在上述文章的基础上,如果您希望每个自定义帖子类型都有特定的元框,您可以使用如下所示的一些 if 语句来实现此目的。
$post_id = ... 设置了 IF 语句所基于的钩子。
Here's a really good articles on creating meta boxes.
Then, building on the above articles, if you want specific meta boxes per custom post type, you can use some if statements like the following to achieve this.
The $post_id = ... sort of sets up the hook that you're IF statement will be based upon.'s
我认为最好的方法是在表单中使用数组。如果您只有保存到 post_meta 的值的集合,您只能将它们链接回帖子(在本例中为房屋),但不能将哪些尺寸与哪种楼层类型相匹配等
如果您有每个表单字段的名称类似的东西
卧室[宽度],卧室[num_windows],卧室[床类型]等,您将在post_meta表中存储一个数组,因此当您获取房屋卧室的后元时,它将返回一个数组数组(第一个级别代表房间,第二个代表房间的属性,
如果您愿意,您甚至可以更进一步,并将字段命名为“卧室[i][属性]”,其中“i”是您在 JS 中维护的计数器。所有卧室只有一块后元
I think the best way to do this would be to use arrays within the forms. If you just had a collection of values which were saved to post_meta, you would only be able to link them back to the post (in this case house) but not which dimensions match which floor type etc
If you had the names of each form field something like
bedroom[width], bedroom[num_windows], bedroom[bed_type] etc, you would store an array within the post_meta table, so when you get the post meta for the bedroom of a house, it would return an array of arrays (the first level represnting the rooms, the second representing the attributes of a room
You could even take it a step further if you wanted, and name the field bedrooms[i][attribute] where i is a counter you maintain within the JS. Then you would only have a single piece of post meta for all the bedrooms