这一切都取决于你想要的元框出现在哪里。如果你想让它出现在帖子
,那么你需要使用
if (! function_exists('add_post_metabox')){
function add_post_metabox(){
add_meta_box('post-meta', esc_html__('My Metabox', 'mytheme'), 'My_Metabox_function', 'post', 'side', 'low');
}
}
add_action('admin_init', 'add_post_metabox');
然后用
if (! function_exists('My_Metabox_function')){
function My_Metabox_function($post){
//metabox layout and variables here
}
}
if (! function_exists('My_Metabox_save_function')){
function My_Metabox_save_function($post_id){
global $post;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return $post_id;
} else{
//do save here
}
}
}
add_action('save_post', 'My_Metabox_save_function');
创建metabox如果你想要页面,那么你可以像上面创建它,但用页面代替帖子
if (! function_exists('add_page_metabox')){
function add_page_metabox(){
add_meta_box('page-meta', esc_html__('My Metabox on Page', 'mytheme'), 'My_Metabox_page_function', 'page', 'normal', 'high');
}
}
add_action('add_meta_boxes', 'add_page_metabox');
你可以勾选admin_init
的add_meta_boxes
钩。有关更多说明,请参阅here。
你可以把它放在你的functions.php
文件,也可以将其设置在单独的.php
文件,并像调用它functions.php
:
require_once(get_template_directory(). '/include/metaboxes.php');
这将包括metaboxes.php
是位于/include
目录的你主题。