这是我的插件代码,用于添加帖子元。问题是:
经过一些测试,我发现只有“老”帖子“复制”了帖子元,而不是最新的帖子。所以说这不是我的主要问题。(如果你对此有解释,我就接受;)
现在的主要问题是:如果我更新一个meta,它不会更新它,而是为相同的post\\u id、相同的meta\\u键以及相同的meta\\u值创建一个新的meta。
我在这里使用教程(以上)中的“保存”功能。
add_action( \'add_meta_boxes\', \'myplugin_add_custom_box\' );
add_action( \'save_post\', \'myplugin_save_postdata\' );
/* Adds a box to the main column on the Post and Page edit screens */
function myplugin_add_custom_box() {
add_meta_box(
\'test1\',
__( \'Author : \', \'myplugin_textdomain\' ),
\'myplugin_inner_custom_box\',
\'post\'
);
add_meta_box(
\'test2\',
__( \'My test2\', \'myplugin_textdomain\' ),
\'myplugin_inner_custom_box\',
\'movies\'
);
}
/* Prints the box content */
function myplugin_inner_custom_box() {
global $post;
echo \'<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="\' .
wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';
$met = get_post_meta($post->ID, \'champ\', true)? get_post_meta($post->ID, \'champ\', true) : \'empty meta\';
/* input */
echo \'<label for="myplugin_new_field">\';
_e("Author name : ", \'myplugin_textdomain\' );
echo \'</label> \';
echo \'<input type="text" id="champ" name="champ" value="\'.$met.\'" />\';
/* select list */
echo \'<label for="my_list_field">\';
_e("Already in database : ", \'myplugin_textdomain\' );
echo \'</label> \';
echo \'<select name="my_list_field" id="my_list_field">\';
global $post;
$s_query = new WP_Query( array(
\'suppress_filters\' => false,
\'post_type\' => \'movies\'));
while($s_query->have_posts()):$s_query->the_post();
$sname = $post->post_title;
$s_output2 =\'\';
$s_output2 .= \'<option value="\'.$post->ID.\'" >\';
$s_output2 .= $post->post_title.\' : allo\';
$s_output2 .= \'</option>\';
echo $s_output2;
endwhile ;
echo \'</select>\';
wp_reset_query();
}
// Save the Metabox Data
function wpt_save_events_meta($post_id, $post) {
if ( !wp_verify_nonce( $_POST[\'eventmeta_noncename\'], plugin_basename(__FILE__) )) {
return $post->ID;
}
if ( !current_user_can( \'edit_post\', $post->ID ))
return $post->ID;
$events_meta[\'champ\'] = $_POST[\'champ\'];
foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array!
$value = implode(\',\', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn\'t have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
}
add_action(\'save_post\', \'wpt_save_events_meta\', 1, 2); // save the custom fields