你想要的是custom meta box.
搜索这个网站也会给你很多例子,但这里有一个简短的概述。
你陷入了add_meta_boxes
适当地打电话,add_meta_box
.
<?php
add_action(\'add_meta_boxes\', \'wpse87567_add_box\');
function wpse87567_add_box($post_type)
{
// $post_type the "page" on which the `add_meta_boxes` action is being fired
// It could be commends or could be a post type. It\'s really only
// useful if you want to dynamically add a meta box everywhere.
add_meta_box(
\'wpse87567-box\', // the meta box ID, can be anything
__(\'A Meta Box\', \'wpse\'), // your meta box title
\'wpse87567_box_callback\', // the callback, the function that spits our your meta box contents (eg. fields)
\'landing_page\', // the post type to which you wish to add the meta box
\'side\', // where should you put it? side, normal, or advanced
\'low\' // how important is it? high, core, default, or low
);
}
function wpse87567_box_callback($post)
{
// post is the current page\'s post object, use it to fetch your additional
// meta data via `get_post_meta` or the like.
// spit out your HTML fields here
}
为了保存东西,你需要
save_post
并检查以确保您没有进行自动保存,帖子类型匹配,nonces签出,并且当前用户可以实际编辑帖子
<?php
add_action(\'save_post\', \'wpse87567_save\', 10, 2);
function wpse87567_save($post_id, $post)
{
// make sure we aren\'t doing an autosave
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
return;
}
// check if your post types match: do you need to save...
if (\'landing_page\' !== $post->post_type) {
return;
}
// If you used a nonce above, and you should have, check it here.
// See if the current user can edit the post
// `edit_post` should be whatever capability your post type was
// registered with. Maybe be `edit_page` or `edit_landing_page`, etc
if (!current_user_can(\'edit_post\', $post_id)) {
return;
}
// if you\'re here, save stuff.
if (!empty($_POST[\'some_field_from_the_callback\'])) {
update_post_meta(
$post_id,
\'_your_meta_key\',
esc_attr(strip_tags($_POST[\'some_field_from_the_callback\']))
);
} else {
delete_post_meta($post_id, \'_your_meta_key\');
}
}