get_template_file()で呼び出す、テンプレートパートファイルに、コードを記述して 特定の場所に、youtubeを一覧で表示するものです。
Raindropsテーマのサンプル用なので、読み替えてください。
hook-after-nav-menu.php(レインドロップステーマで、メニュー下部に表示するためのテンプレートで、ファイル名が一致すれば自動的にインクルートします。)
<?php
global $shortcode_tags;
$args= array('width'=> 300, 'height'=> 200);
if ( have_posts() ) {
$youtube= array();
while( have_posts() ) {
the_post();
preg_match('!(https?:\/\/)?(www\.)?(youtube\.com |youtu\.?be)\/[A-z0-9]+!', get_the_content( ) ,$matches);
if( ! empty( $matches )){
$youtube[]= esc_url( $matches[0] );
}
}
rewind_posts();
}
$result='<div style="width:100%;height:200px;border:1px solid #ccc;padding-top:1em;">';
foreach($youtube as $tube){
$result .= '<div style="display:inline-block;margin-right:1em">';
//$result .= wp_oembed_get( $tube , $args ); or
$attr= array( 'src'=> $tube, 'width'=> 300,'height'=> 200 );
$result .= wp_video_shortcode( $attr );
$result .= '</div>';
}
$result .= '</div>';
echo $result;
?>
