这与其说是一个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]