I have videos set up as a custom post type and music, commercial, promo set up as categories within this post type:
I have a function that displays a custom meta box on the videos post type pages in the wordpress back end. The user is able to enter the ID of a youtube video or the ID of a vimeo video - wordpress then displays the video for the ID on custom post type page. When a user adds a new post to the videos custom post type and assigns it to any of the categories I specify I want wordpress to display the different videos. The code I have at the moment is not doing what I want it to do because it is displaying the same video on each post even though an ID hasnt been specified on some of them. For example on the music post page I have assigned it the category music and put a vimeo video ID which is displayed on the front end but then the same video is displayed for promo and commercial and I dont want that to happen. The loop I have running this is (in single-videos.php):
<?php
$args = array( 'post_type' => 'videos', 'posts_per_page' => 20, 'orderby' => 'date', 'order' => 'ASC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
//$args = array( 'post_type' => 'videos', 'posts_per_page' => 20, 'orderby' => 'date', 'order' => 'ASC' );
$ytubeID = get_post_meta($post->ID, '_youtubeID', true);
$vimID = get_post_meta($post->ID, '_vimeoID', true);
if ($ytubeID || $vimID){
if ($ytubeID){
echo '<iframe title="YouTube video player" class="youtube-player" type="text/html" src="http://www.youtube.com/embed/'.$ytubeID.'" allowfullscreen="true" frameborder="0" width="640" height="390">';
echo '</iframe>';
} elseif ($vimID){
echo '<br />';
echo '<iframe src="http://player.vimeo.com/video/'.$vimID.'" width="640" height="390" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
}//end if yutbeID or vimIDthe_excerpt(); //excerpt added for information
}
endwhile;
wp_reset_query();
?>
Below is the full function that creates the meta box & stores the information:
// Create the Video Information Meta Box by hooking into the admin menu for a post
add_action('admin_menu', '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, $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(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
} //end save_video_meta
//save the video custom fields
add_action('save_post', 'save_video_meta', 1, 2);