ผมใช้ทำกับ งานล่าสุด เอาแบ่งปันกันครับ
1. อันนี้วางที่ function.php
// custom meta box video id
add_action( 'add_meta_boxes', 'cs_mb_video_id' );
function cs_mb_video_id() {
add_meta_box( 'video-id', 'Video ID:', 'cs_mb_video_function', 'post', 'normal', 'high');
}
// callback function
function cs_mb_video_function( $post ) {
// retreive the meta-data values if they exist
$cs_mb_video = get_post_meta( $post->ID, '_cs_mb_video', true );
$cs_mb_vimeo_id = get_post_meta( $post->ID, '_cs_mb_vimeo_id', true );
?>
<p>Youtube ID: <input type="text" style="width:300px;" name="cs_mb_video" value="<?php echo esc_attr( $cs_mb_video ); ?>" /></p>
<p>Vimeo ID: <input type="text" style="width:300px;" name="cs_mb_vimeo_id" value="<?php echo esc_attr( $cs_mb_vimeo_id ); ?>" /></p>
<?php }
add_action( 'save_post', 'cs_mb_id_save');
function cs_mb_id_save( $post_id ) {
// verify the metadata is set
if ( isset( $_POST['cs_mb_video'] ) ) {
// save the metadata
update_post_meta( $post_id, '_cs_mb_video', strip_tags( $_POST['cs_mb_video'] ) );
}
if ( isset( $_POST['cs_mb_vimeo_id'] ) ) {
// save the metadata
update_post_meta( $post_id, '_cs_mb_vimeo_id', strip_tags( $_POST['cs_mb_vimeo_id'] ) );
}
}
2. อันนี้ใส่ที่หน้าที่ต้องการจะโชว์ page, single, category, index แล้วแต่ครับ
(อันนี้เป็น VIDEO นะครับ)
<?php while(have_posts()) : the_post(); ?>
<?php
$youtube_id = get_post_meta(get_the_ID(), '_cs_mb_video', true);
$vimeo_id = get_post_meta(get_the_ID(), '_cs_mb_vimeo_id', true);
?>
<?php
if ($youtube_id != '') { ?>
<div id="vdo_container">
<iframe width="720" height="405" src="
http://www.youtube.com/embed/ 
<?php echo $youtube_id; ?>" frameborder="0" allowfullscreen></iframe>
</div>
<?php } elseif ($vimeo_id != '') { ?>
<div id="vdo_container">
<iframe src="
http://player.vimeo.com/video/ 
<?php echo $vimeo_id; ?>" width="720" height="405" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>
?>
<?php endwhile; ?>
ส่วนนี้หน้าปก หรือ thumbnail ของ video
<?php while(have_posts()) : the_post(); ?>
<?php
$youtube_id = get_post_meta(get_the_ID(), '_cs_mb_video', true);
$vimeo_id = get_post_meta(get_the_ID(), '_cs_mb_vimeo_id', true);
?>
<?php if ($youtube_id != '') { ?>
<img src="
http://img.youtube.com/vi/ 
<?php echo $youtube_id; ?>/0.jpg" width="220" height="145" />
<?php } elseif ($vimeo_id != '') { ?>
<?php $vimeo_thumb = json_decode(file_get_contents('
http://vimeo.com/api/v2/video/' 
. $vimeo_id . '.json'));?>
<img src="<?php echo $vimeo_thumb[0]->thumbnail_medium ?>" width="220" height="145" />
<?php } ?>
<?php endwhile; ?>
ลองดูนะครับ ช่วยกันๆ