我會假設你對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