我认为您将错误的post ID值传递给get_post_meta()
作用尝试以下操作:
$ytubeID = get_post_meta(get_the_ID(), \'_youtubeID\', true);
$vimID = get_post_meta(get_the_ID(), \'_vimeoID\', true);
Note:, 由于您正在使用WP\\u Query的新实例,因此不需要
wp_reset_query();
, 使用
wp_reset_postdata();
相反
在添加用于存储自定义字段值的完整代码之前,我只能说,您再次向update_post_meta()
和delete_post_meta()
函数。。通常,post ID会传递给save函数回调,而$post->ID是错误的(您正在进行的$post->post\\u类型检查也是错误的)。您还将向save回调传递第二个参数,但save回调只接受一个参数。尝试以下操作:
add_action( \'save_post\', \'my_save_postdata\' );
function my_save_postdata($post_id){
$video_meta[\'_youtubeID\'] = $_POST[\'_youtubeID\'];
$video_meta[\'_vimeoID\'] = $_POST[\'_vimeoID\'];
foreach ($video_meta as $key => $value) { // Cycle through the $video_meta array
if( $_POST[\'post_type\'] == \'revision\' ) return; // Don\'t store custom data twice
$value = implode(\',\', (array)$value); // If $value is an array, make it a CSV
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
}//endforeach video meta
}
您可以进行进一步的改进,但它们超出了答案的范围,我举个例子,以下代码:
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
可以是:
if($value) {
update_post_meta($post_id, $key, $value);
} else {
delete_post_meta($post_id, $key); // Delete if blank
}
因为
update_post_meta()
将检查自定义字段是否已经有值并将其更新,如果自定义字段没有值(键不存在),则函数将调用
add_post_meta()
.
代码中的所有上述内容:
// Create the Video Information Meta Box by hooking into the admin menu for a post
// Add the meta boxes in the add_meta_boxes action hook, no in admin menu
add_action(\'add_meta_boxes\', \'video_add_box\');
//Function call by the "add_action" to tell it what to add, "video_information" and where "post"
function video_add_box(){
add_meta_box(\'video_information\', \'Video Information\', \'video_information\', \'videos\', \'normal\', \'high\');
}
//function to populate the meta box added above
function video_information(){
global $post;
// Noncename needed to verify where the data originated
echo \'<input type="hidden" name="video_noncename" id="video_noncename" value="\' .
wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';
//adds the custom field _youtubeID plus some other stuff
$youtubeID = get_post_meta($post->ID, \'_youtubeID\', true);
if ( empty($youtubeID) ) {
$youtubeID = \'\';
}
//adds the custom field _vimeoID
$vimeoID = get_post_meta($post->ID, \'_vimeoID\', true);
if ( empty($vimeoID) ) {
$vimeoID = \'\';
}
//add the box
echo \'<br />\';
echo \'<strong>Youtube ID:</strong> <input type="text" name="_youtubeID" value="\' . $youtubeID . \'" size="20" maxlength="30" />\';
echo \'<br />\';
echo \'<strong>Vimeo ID:</strong> <input type="text" name="_vimeoID" value="\' . $vimeoID . \'" size="20" maxlength="30" />\';
echo \'<br />\';
} //end video_information function
//save_video_meta is called below with the action "save_post" and saves your IDs to the post
function save_video_meta($post_id) {
// the $post parameter you wass passing has any effect, the save
// action only accept one parameter. If you need all the post data beeing saved
// you can access to the global $post_data; or to the superglobal $_POST
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST[\'video_noncename\'], plugin_basename(__FILE__) )) {
return $post->ID;
}
// Is the user allowed to edit the post or page?
if ( !current_user_can( \'edit_post\', $post->ID )){
return $post->ID;
}
$video_meta[\'_youtubeID\'] = $_POST[\'_youtubeID\'];
$video_meta[\'_vimeoID\'] = $_POST[\'_vimeoID\'];
foreach ($video_meta as $key => $value) { // Cycle through the $video_meta array
if( $_POST[\'post_type\'] == \'revision\' ) return; // Don\'t store custom data twice
$value = implode(\',\', (array)$value); // If $value is an array, make it a CSV
if($value) {
update_post_meta($post_id, $key, $value);
} else {
delete_post_meta($post_id, $key); // Delete if blank
}
}//endforeach video meta
} //end save_video_meta
//save the video custom fields
add_action(\'save_post\', \'save_video_meta\', 1, 2);
Just a reminder: Wordpress可以处理大量的外部视频提供商,如youtube和vimeo。只需将视频URL粘贴到编辑器框中,它就会自动显示在前端。此外,还有一个内置的分类法“post format”,可以让您通过“视频格式”过滤帖子。简言之,我认为完全不需要用自定义元框注册一个新的帖子类型,只需显示“视频类型”的帖子;使用内置特性,只需很少的额外代码即可实现相同的功能。