我在上找到了解决方案Denis Žoljom\'s post
1) -我添加了metabox
像这样
add\\u操作(“add\\u meta\\u box”、“mytheme\\u add\\u meta\\u box”);
if ( ! function_exists( \'mytheme_add_meta_box\' ) ) {
function mytheme_add_meta_box(){
add_meta_box( \'header-page-metabox-options\', esc_html__(\'Header Color\', \'mytheme\' ), \'mytheme_header_meta_box\', \'page\', \'side\', \'low\');
}
}
2)-将颜色选择器样式和脚本排队
add_action( \'admin_enqueue_scripts\', \'mytheme_backend_scripts\');
if ( ! function_exists( \'mytheme_backend_scripts\' ) ){
function mytheme_backend_scripts( $hook ) {
wp_enqueue_style( \'wp-color-picker\');
wp_enqueue_script( \'wp-color-picker\');
}
}
3)-为颜色选择器、一些描述文本和输入字段创建HTML容器
if ( ! function_exists( \'mytheme_header_meta_box\' ) ) {
function mytheme_header_meta_box( $post ) {
$custom = get_post_custom( $post->ID );
$header_color = ( isset( $custom[\'header_color\'][0] ) ) ? $custom[\'header_color\'][0] : \'\';
wp_nonce_field( \'mytheme_header_meta_box\', \'mytheme_header_meta_box_nonce\' );
?>
<script>
jQuery(document).ready(function($){
$(\'.color_field\').each(function(){
$(this).wpColorPicker();
});
});
</script>
<div class="pagebox">
<p><?php esc_attr_e(\'Choosse a color for your post header.\', \'mytheme\' ); ?></p>
<input class="color_field" type="hidden" name="header_color" value="<?php esc_attr_e( $header_color ); ?>"/>
</div>
<?php
}
}
4)-保存元框
if ( ! function_exists( \'mytheme_save_header_meta_box\' ) ) {
function mytheme_save_header_meta_box( $post_id ) {
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {
return;
}
if( !current_user_can( \'edit_pages\' ) ) {
return;
}
if ( !isset( $_POST[\'header_color\'] ) || !wp_verify_nonce( $_POST[\'mytheme_header_meta_box_nonce\'], \'mytheme_header_meta_box\' ) ) {
return;
}
$header_color = (isset($_POST[\'header_color\']) && $_POST[\'header_color\']!=\'\') ? $_POST[\'header_color\'] : \'\';
update_post_meta($post_id, \'header_color\', $header_color);
}
}
add_action( \'save_post\', \'mytheme_save_header_meta_box\' );
Yeeeeeh)))希望这能帮助其他人;)