在主页上添加最新的YouTube视频

时间:2019-12-07 作者:Mohammed Jacksi

有没有办法从我的Youtube频道获取最新的Youtube视频,并在Wordpress页面的视频库中显示主题?

1 个回复
最合适的回答,由SO网友:Steffen Görg 整理而成

这与其说是一个wordpress问题,不如说是一个youtube问题。但您可以编写一个简单的短代码函数并将其添加到您的站点。

// Add Shortcode
   function custom_youtubeapishortcode()
   {
     $playlistid = "XXXX";
     $apikey = "XXXX";


     $curl = curl_init();

     curl_setopt_array($curl, array(
     CURLOPT_URL => "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=".$playlistid."&maxResults=1&key=".$apikey."",
     CURLOPT_RETURNTRANSFER => true,
     CURLOPT_ENCODING => "",
     CURLOPT_MAXREDIRS => 10,
     CURLOPT_TIMEOUT => 30,
     CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
     CURLOPT_CUSTOMREQUEST => "GET",
     CURLOPT_HTTPHEADER => array(
     "Accept: */*",
     "Accept-Encoding: gzip, deflate",
     "Cache-Control: no-cache",
     "Connection: keep-alive",
     "Host: www.googleapis.com",
     "cache-control: no-cache"
   ),
  ));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    $array = json_decode($response, true);
    return apply_filters(\'the_content\', "https://youtu.be/".$array[\'items\'][\'0\'][\'snippet\'][\'resourceId\'][\'videoId\']);
}
 }
 add_shortcode(\'youtubeapishortcode\', \'custom_youtubeapishortcode\');
您应该从google开发者那里获得youtube的api密钥:https://console.developers.google.com/

将api密钥和youtube播放列表ID添加到代码中,并将其添加到函数中。php

在任何页面上调用快捷码:

 [youtubeapishortcode]