Youtubeフィードを使用してWordPressにチャンネルの動画一覧を表示する。

  • Updated: 2023.05.04
  • Published: 2023.05.04
  • 1,104views
  • API
  • RSS
  • WordPress
  • YouTube
  • フィード

概要

Youtubeにはフィードを取得する方法があります。

今回はYoutubeフィードを使用してWordPressにチャンネルの動画一覧を表示させます。


サンプルページ


フィードURL

https://www.youtube.com/feeds/videos.xml?channel_id=チャンネルID

チャンネルIDは以前投稿した記事で確認することができます。

ソース

今回は固定ページの独自テンプレートに表示するようにします。

読み込み側

<?php
/**
 * Template Name: YouTubeクリエーター表示用テンプレート
 * Template Post Type: page
 *
 * @package WordPress
 * @subpackage Twenty_Twenty
 * @since Twenty Twenty 1.0
 */

$page = 100;
$transient_name = 'youtube_videos';
$url = 'https://www.youtube.com/feeds/videos.xml?channel_id=チャンネルID';

if ( false === ( $youtube_creator = get_transient( $transient_name ) ) ) {
	$jary = array();

	$feed = fetch_feed( $url );

	if ( ! is_wp_error( $feed ) ) :
		// すべてのフィードから最新を出力します。
		$maxitems = $feed->get_item_quantity( $page );
		// 0件から始めて指定した件数までの配列を生成します。
		$rss_items = $feed->get_items( 0, $maxitems );
	endif;

	foreach ( $rss_items as $key => $item ) :
		if ($enclosure = $item->get_enclosure()){
			$community = $item->data['child']["http://search.yahoo.com/mrss/"]["group"][0]["child"]["http://search.yahoo.com/mrss/"]["community"][0]["child"]["http://search.yahoo.com/mrss/"];
			$views = $community["statistics"][0]["attribs"][""]["views"];
			$rating = $community["starRating"][0]["attribs"][""];

			$videoId = $item->data['child']["http://www.youtube.com/xml/schemas/2015"]["videoId"][0]["data"];
			$channelId = $item->data['child']["http://www.youtube.com/xml/schemas/2015"]["channelId"][0]["data"];

			$ary = array(
				'title' => esc_html($item->get_title()),
				'url' => esc_html($item->get_permalink()),
				'img' => esc_html($enclosure->get_thumbnail()),
				'description' => esc_html( strip_tags($enclosure->get_description())),
				'date' => esc_html($item->get_date('Y年m月d日 | g:i a')),
				'updated_date' => esc_html($item->get_updated_date('Y年m月d日 | g:i a')),
				'videoId' => esc_html($videoId),
				'channelId' => esc_html($channelId),
				'channel_name' => esc_html($item->get_author()->name),
				'channel_url' => esc_html($item->get_author()->link),
				'rating_count' => esc_html($rating['count']),
				'rating_average' => esc_html($rating['average']),
				'views' => esc_html($views),
			);
		}


		$jary[] = $ary;
	endforeach;

	$youtube_creator = $jary;

	set_transient( $transient_name, $youtube_creator, 2 * HOUR_IN_SECONDS );
}
?>

表示側

      <ul class="archive-list">
	      <?php foreach ($youtube_creator as $post): ?>
            <li>
              <a href="<?php print $post['url'] ;?>" class="item" target="_blank">
                <figure>
                  <img src="<?php print $post['img'] ;?>" alt="">
                </figure>
                <div class="tit"><?php print $post['title'] ;?></div>
                <div class="description"><?php print $post['description'] ;?></div>
                <div class="date"><?php print $post['views'] ;?>view  <?php print $post['date'] ;?></div>
              </a>
            </li>
	      <?php endforeach; ?>
      </ul>

関連記事

人気の投稿

最新の投稿

タグ

月別アーカイブ

Contact

WEB制作の依頼など気軽にお問い合わせください。

お問い合わせ