你好@George Wiscombe:
如此接近,却又如此遥远(我知道痛苦,我经常在那里。:)你差点就受了,但使用了错误的挂钩来进行后期元更新。使用\'wp_insert_post_data\'
钩子而不是\'save_post\'
挂钩:
<?php
if (!class_exists(\'YourSite_Portfolio\')) {
class YourSite_Portfolio {
static function on_load() {
add_action(\'init\',array(__CLASS__,\'init\'));
add_action("admin_init",array(__CLASS__,\'admin_init\'));
add_action(\'wp_insert_post_data\',array(__CLASS__,\'wp_insert_post_data\'),10,2);
}
static function init() {
register_post_type( \'portfolio\',array(
\'label\' => __(\'Portfolio\'),
\'singular_label\' => __(\'Portfolio\'),
\'public\' => true,
\'show_ui\' => true,
\'menu_position\' => 5,
\'capability_type\' => \'page\',
\'hierarchical\' => false,
\'rewrite\' => true,
\'show_in_nav_menus\' => true,
\'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'excerpt\'),
\'has_archive\' => true
));
register_taxonomy( \'type\', \'portfolio\', array(
\'hierarchical\' => false,
\'label\' => __(\'Project Type\'),
\'query_var\' => \'type\',
\'rewrite\' => array(\'slug\' => \'portfolio/type\' )
));
}
static function admin_init(){
add_meta_box(\'portfolio_text\',\'Main Text\',array(__CLASS__,\'portfolio_options\'),\'portfolio\',\'normal\',\'high\');
}
static function portfolio_options($post,$metabox) {
$portfolio_text = get_post_meta($post->ID,\'portfolio_text\',true);
$html =<<<HTML
<textarea name="portfolio_text" cols="40" rows="1" style="width:98%; height:100px"/>{$portfolio_text}</textarea>
HTML;
echo $html;
}
static function wp_insert_post_data($data,$postarr) {
if ($postarr[\'post_type\'] == \'portfolio\') {
update_post_meta($postarr[\'ID\'], \'portfolio_text\', $postarr[\'portfolio_text\']);
}
return $data;
}
}
YourSite_Portfolio::on_load();
}
The
\'wp_insert_post_data\'
钩子是WordPress从
$_POST
大堆当你到达
\'save_post\'
WordPress重新加载了帖子,这就是为什么
\'portfolio_text\'
尝试保存时清空。