我会假设你对PHP感到满意,因为你在这里说了什么。好消息是您的PHP知识将在这里派上用场。是时候学习register_post_type函数的新功能了;即register_meta_box_cb
参数。这使您可以定义一个函数来调用以在cpt添加和编辑页面上添加元框。举例来说,这里是一个CPT(从手抄本直取)与所添加的参数:
add_action('init', 'codex_custom_init');
function codex_custom_init()
{
$labels = array(
'name' => _x('Books', 'post type general name'),
'singular_name' => _x('Book', 'post type singular name'),
'add_new' => _x('Add New', 'book'),
'add_new_item' => __('Add New Book'),
'edit_item' => __('Edit Book'),
'new_item' => __('New Book'),
'view_item' => __('View Book'),
'search_items' => __('Search Books'),
'not_found' => __('No books found'),
'not_found_in_trash' => __('No books found in Trash'),
'parent_item_colon' => '',
'menu_name' => 'Books'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'register_meta_box_cb' => 'my_meta_box_function',
'supports' => array('title','editor','author','thumbnail','excerpt','comments')
);
register_post_type('book',$args);
}}
现在,你有元盒CB的功能定义,使用它像:
function my_beta_box_function(){
add_meta_box(
'myplugin_sectionid',
__('My Post Section Title', 'myplugin_textdomain'),
'myplugin_inner_custom_box',
'book'
);
}
的add_meta_box为您定义了一个元框,并提供了一个在metabox内创建内容的功能。在这种情况下,我们的代码指的是功能myplugin_inner_custom_box
。有关添加元框的更多信息,请参阅http://codex.wordpress.org/Function_Reference/add_meta_box。所以,我们现在需要通过定义功能来添加我们的内容:
function myplugin_inner_custom_box()
{
// Everything here would appear in the meta box
}
在这一点上,你可以利用你的PHP知识来创建可以在后处理提交的HTML表单。您可以添加您的+按钮并使用您的PHP,就像您在任何其他PHP应用程序中一样。我不会去做如何做到这一点,因为我会假设你可以做到这一点。实际上,我已经创建了一些这样的metabox,并且知道我发布的函数之后,我可以依靠我的PHP知识。最后一块是保存输入。通过使用定义您的日常保存和save_post动作的功能,你可以用你的PHP知识来保存数据:
/* Do something with the data entered */
add_action('save_post', 'myplugin_save_postdata');
/* When the post is saved, saves our custom data */
function myplugin_save_postdata($post_id) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if (!wp_verify_nonce($_POST['myplugin_noncename'], plugin_basename(__FILE__)))
return $post_id;
// Check permissions
if ('page' == $_POST['post_type'])
{
if (!current_user_can('edit_page', $post_id))
return $post_id;
}
else
{
if (!current_user_can('edit_post', $post_id))
return $post_id;
}
// OK, we're authenticated: we need to find and save the data
$mydata = $_POST['myplugin_new_field'];
update_post_meta('my_field', $mydata);
return $mydata;
}
最后,在保存功能的说明。如果您使用数组值(例如,docs[]
),$ _POST ['docs']的值将是一个数组。幸运的是,WP的update_post_meta
函数都设置为处理数组输入。它会序列化并输入它。使用兼容的get_post_meta
函数将反序列化数组以供数据输出使用。
祝你好运!
哇谢谢你!让我一步一步阅读。再次感谢! – drpcken 2011-04-29 16:18:17
所以这些元框没有连接到任何自定义字段吗?我将不得不创建我自己的表为metaboxes的值并将它们链接到帖子ID? – drpcken 2011-04-29 17:05:48
哦,等等,我想我明白了,它是动态添加自定义元框!非常好!我会与此合作。谢谢! – drpcken 2011-04-29 18:18:12