如何将自定义字段添加到自定义帖子类型?
-
-
使用http://wordpress.org/extend/plugins/types/Use http://wordpress.org/extend/plugins/types/
- 0
- 2012-07-30
- Ajay Patel
-
6 个回答
- 投票数
-
- 2011-05-13
这可能比您想象的要复杂,我将使用框架进行研究:
如果您想编写自己的书,请参考以下一些不错的教程:
This is probably more complicated than you think, I would look into using a framework:
If you want to write your own , here are some decent tutorials:
-
真的会很难.我认为这就像将邮政代码添加到我的函数中一样简单,就像我们处理帖子类型和分类法一样.really it would be that hard. I thought it would be as simple as adding a register code to my functions like we do with post types and taxonomies.
- 1
- 2011-05-13
- xLRDxREVENGEx
-
我将为这个答案加一个,但这并不复杂.thinkvitamin.com链接很好地解释了如何添加和保存元框.sltaylor.co.uk链接是有关使用一些出色的编码实践的出色教程.我的警告是在使用`save_post`钩子时要小心.它被称为在怪异的时间.确保将WP_DEBUG变量设置为true,以查看使用它时可能出现的错误.I'll plus one this answer, but it's not too complex. The thinkvitamin.com link does a great job explaining how to add the metaboxes and save them. The sltaylor.co.uk link is an awesome tutorial on using some great coding practices. My word of caution is be careful when using the `save_post` hook. It's called at weird times. Make sure to have WP_DEBUG variable set to true in order to see potential errors that arise when using it.
- 1
- 2011-05-13
- tollmanz
-
只是我使用了thinkvitamin链接进行了一次更新,对您的工作大有帮助,这对于设置自定义字段很容易Just an update i used the thinkvitamin link and that helped tremendously and it was a cake walk on setting up custom fields
- 2
- 2011-05-13
- xLRDxREVENGEx
-
- 2013-04-23
添加/编辑
supports
参数(使用register_post_type
时)包括custom-fields
以发布您自定义帖子类型的编辑屏幕:'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', 'revisions' )
来源: https://codex.wordpress.org/Using_Custom_Fields#Displaying_Custom_Fields
Add/edit the
supports
argument ( while usingregister_post_type
) to include thecustom-fields
to post edit screen of you custom post type:'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', 'revisions' )
Source: https://codex.wordpress.org/Using_Custom_Fields#Displaying_Custom_Fields
-
您能解释一下为什么这可以解决问题吗?Can you please explain why this could solve the issue?
- 2
- 2013-04-23
- s_ha_dum
-
是的,这可行. 谁-1回答了.你能把它取回来吗? 问候,Yes, this works. Who -1'ed the answer. Can you please take it back? Regards,
- 1
- 2013-07-25
- Junaid Qadir
-
...接着.........?...and then.........?
- 8
- 2016-10-26
- Mark
-
- 2014-01-30
尽管您必须添加一些验证,但是对于当前版本的WordPress,此操作似乎并不复杂.
基本上,您需要两个步骤将"自定义字段"添加到"自定义帖子类型":
- 创建一个保存您的"自定义字段"的metabox
- 将您的"自定义字段"保存到数据库中
这些步骤在此处进行了全局描述: http://wordpress.org/support/topic/is-it-possible-to-add-an-extra-field-to-a-custom-post-type
示例:
将名为"功能"的自定义字段添加到名为"prefix-teammembers"的自定义帖子类型中.
首先添加元框:
function prefix_teammembers_metaboxes( ) { global $wp_meta_boxes; add_meta_box('postfunctiondiv', __('Function'), 'prefix_teammembers_metaboxes_html', 'prefix_teammembers', 'normal', 'high'); } add_action( 'add_meta_boxes_prefix-teammembers', 'prefix_teammembers_metaboxes' );
如果您添加或编辑"前缀团队成员",则会触发
add_meta_boxes_{custom_post_type}
挂钩.有关add_meta_box()
函数.在上面的add_meta_box()
调用中,是prefix_teammembers_metaboxes_html
,这是添加表单字段的回调:function prefix_teammembers_metaboxes_html() { global $post; $custom = get_post_custom($post->ID); $function = isset($custom["function"][0])?$custom["function"][0]:''; ?> <label>Function:</label><input name="function" value="<?php echo $function; ?>"> <?php }
第二步,将您的自定义字段添加到数据库.保存
save_post_{custom_post_type}
钩子后会触发(从3.7版开始,请参见: https://stackoverflow.com/questions/5151409/wordpress-save-post-action-for-custom-posts ).您可以挂钩以保存您的自定义字段:function prefix_teammembers_save_post() { if(empty($_POST)) return; //why is prefix_teammembers_save_post triggered by add new? global $post; update_post_meta($post->ID, "function", $_POST["function"]); } add_action( 'save_post_prefix-teammembers', 'prefix_teammembers_save_post' );
Although you should have to add some validation, this action does not seem to be complicated for the current version of WordPress.
Basically you need two steps to add a Custom Field to a Custom Post Type:
- Create a metabox which holds your Custom Field
- Save your Custom Field to the database
These steps are globally described here: http://wordpress.org/support/topic/is-it-possible-to-add-an-extra-field-to-a-custom-post-type
Example:
Add a Custom Field called "function" to a Custom Post Type called "prefix-teammembers".
First add the metabox:
function prefix_teammembers_metaboxes( ) { global $wp_meta_boxes; add_meta_box('postfunctiondiv', __('Function'), 'prefix_teammembers_metaboxes_html', 'prefix_teammembers', 'normal', 'high'); } add_action( 'add_meta_boxes_prefix-teammembers', 'prefix_teammembers_metaboxes' );
If your add or edit a "prefix-teammembers" the
add_meta_boxes_{custom_post_type}
hook is triggered. See http://codex.wordpress.org/Function_Reference/add_meta_box for theadd_meta_box()
function. In the above call ofadd_meta_box()
isprefix_teammembers_metaboxes_html
, a callback to add your form field:function prefix_teammembers_metaboxes_html() { global $post; $custom = get_post_custom($post->ID); $function = isset($custom["function"][0])?$custom["function"][0]:''; ?> <label>Function:</label><input name="function" value="<?php echo $function; ?>"> <?php }
In the second step you have your custom field to the database. On saving the
save_post_{custom_post_type}
hook is triggered (since v 3.7, see: https://stackoverflow.com/questions/5151409/wordpress-save-post-action-for-custom-posts). You can hook this to save your custom field:function prefix_teammembers_save_post() { if(empty($_POST)) return; //why is prefix_teammembers_save_post triggered by add new? global $post; update_post_meta($post->ID, "function", $_POST["function"]); } add_action( 'save_post_prefix-teammembers', 'prefix_teammembers_save_post' );
-
"为什么为什么add_new会触发prefix_teammembers_save_post?"您找到答案了吗,我也绊在了我不记得的额外功能触发器上?"why is prefix_teammembers_save_post triggered by add new?" have you found an answer, i am also stumbling on a extra function trigger which i can't recall?
- 0
- 2015-02-18
- alex
-
"将名为"功能"的自定义字段添加到名为"prefix-teammembers"的自定义帖子类型中.""被称为"是什么意思?名称?singular_name?标签?也许是用作register_post_type中第一个参数的字符串函数,或者只要它是一致的,也许无关紧要."Add a Custom Field called 'function" to a Custom Post Type called 'prefix-teammembers'." What does "called" mean? The name? The singular_name? The label? Maybe it's the string used as the first argument in the register_post_type function. Or maybe it doesn't matter what it is so long as it's consistent.
- 0
- 2019-10-07
- arnoldbird
-
- 2018-01-03
自定义元框和自定义字段有各种插件.如果您查看的是针对开发人员的插件,则应尝试元框.它轻巧而且功能强大.
如果您正在寻找有关如何为元框/自定义字段编写代码的教程,请这是一个好的开始.这是该系列文章的第一部分,可以帮助您优化代码以使其易于扩展.
There are various plugins for custom meta boxes and custom fields. If you look at a plugin that focuses on developers, then you should try Meta Box. It's lightweight and very powerful.
If you're looking for a tutorial on how to write code for a meta box / custom fields, then this is a good start. It's the first part of a series that might help you refine the code to make it easy to extend.
-
- 2020-08-12
我知道这个问题很旧,但有关该主题的更多信息
WordPress内置了对自定义字段的支持.如果您具有自定义帖子类型,那么您所要做的就是在" register_post_type"内部的支持数组中包含" custom-fields"(由@kubante回答)
注意,该选项还适用于本机帖子类型,例如您只需要打开它的帖子和页面即可
现在,此自定义字段非常基础,并且接受字符串作为值.在很多情况下都可以,但是对于更复杂的字段,我建议您使用"高级自定义字段"插件
I know this question is old but for more info about the topic
WordPress has built-in support for custom fields. If you have a custom post type then all you need is to include 'custom-fields' inside the support array inside of register_post_type as answered by @kubante
Note that this option is also available for native post types like posts and pages you just need to turn it on
Now This custom field is very basic and accepts a string as a value. In many cases that's fine but for more complex fields, I advise that you use the 'Advanced Custom Fields' plugin
-
- 2017-10-28
// slider_metaboxes_html , function for create HTML function slider_metaboxes( ) { global $wp_meta_boxes; add_meta_box('postfunctiondiv', __('Custom link'), 'slider_metaboxes_html', 'slider', 'normal', 'high'); } //add_meta_boxes_slider => add_meta_boxes_{custom post type} add_action( 'add_meta_boxes_slider', 'slider_metaboxes' );
完美的知识
// slider_metaboxes_html , function for create HTML function slider_metaboxes( ) { global $wp_meta_boxes; add_meta_box('postfunctiondiv', __('Custom link'), 'slider_metaboxes_html', 'slider', 'normal', 'high'); } //add_meta_boxes_slider => add_meta_boxes_{custom post type} add_action( 'add_meta_boxes_slider', 'slider_metaboxes' );
Perfect knowledge
好,所以我已经注册了一些自定义帖子类型和一些分类法.现在,为了我的一生,我无法弄清楚我需要在"自定义帖子类型"中添加"自定义字段"的代码.
我需要一个下拉菜单和一个单行文本区域.但是我还需要为帖子类型设置单独的字段.因此,假设发布类型1有3个字段,发布类型2有4个字段,但是这些字段是不同的.
任何提示都可以帮助我查看编解码器并发现一些内容,但无法理解我需要添加到
中的内容functions.php
文件