2014-06-15 143 views
1

試圖在我的新WordPress的Youtube利基主題做一些事情。我的主題中有一個標籤供用戶輸入Youtube手錶?v = xxxxxxxxxxx ID。當他們輸入xxxxxxxxxxx網址時,會輸出視頻,通過'循環'查詢顯示每個標籤16個帖子。Youtube獲得縮略圖

工作實例here

我已經看過這個谷歌開發者控制檯API的事情......這是完整的胡言亂語給我。我使用自定義的帖子類型爲您在我的網站上看到的4個類別。只有「喜劇」現在正在人口稠密。

新編輯:這個作品。謝謝所有幫助過我的人!

<?php 
    $new_query = new WP_Query(); 
    $new_query->query(array('post_type' => array('comedy'), 'orderby' => 'title', 'order' => 'asc','showposts' => 16, 'paged'=>$paged)); 

    while ($new_query->have_posts()) : $new_query->the_post(); 

    $url = get_post_meta ($post->ID, 'comedy_url', $single = true); 

    include(TEMPLATEPATH . '/library/variables.php'); 

    $code = 'http://www.youtube.com/watch?v=' . $url; 
    $json = file_get_contents('http://www.youtube.com/oembed?url='.urlencode($code)); 
    $video = json_decode($json); 
?> 

<img src="<?php echo $video->thumbnail_url ?>" /> 

在拉動縮略圖時,您可能會注意到縮略圖頂部和底部的BLACK BORDERS。爲了解決這個問題使用下面的代碼:

<a href="<? if($code) {?><?php echo $code; ?><? } ?>"> 
    <div class="yt-thumbnail" style="background:url('<?php echo $video->thumbnail_url ?>') center no-repeat;"></div> 
</a> 

// in your css file 
.yt-thumbnail{ 
    height: 164px; 
    width: 300px; 
    background-size: 300px 220px; 
} 

回答

1

您可以使用透過oEmbed API來獲取縮略圖,上傳者的姓名和頭銜。所有你需要的是編碼視頻的網址,例如:

http://www.youtube.com/oembed?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D9FOWI6Zftpg 

這將返回一個JSON響應:

{ 
    "provider_name":"YouTube", 
    "version":"1.0", 
    "author_url":"http:\/\/www.youtube.com\/user\/NScherzingerVEVO", 
    "type":"video", 
    "author_name":"NScherzingerVEVO", 
    "thumbnail_url":"http:\/\/i1.ytimg.com\/vi\/9FOWI6Zftpg\/hqdefault.jpg", 
    "provider_url":"http:\/\/www.youtube.com\/", 
    "title":"Nicole Scherzinger - Your Love", 
    "height":270, 
    "width":480, 
    "thumbnail_height":360, 
    "html":"\u003ciframe width=\"480\" height=\"270\" src=\"http:\/\/www.youtube.com\/embed\/9FOWI6Zftpg?feature=oembed\" frameborder=\"0\" allowfullscreen\u003e\u003c\/iframe\u003e", 
    "thumbnail_width":480 
} 

獲得的觀看次數是有點難度,看答案here瞭解詳情。

編輯:

下面是一個例子如何讓PHP中的縮略圖:

<?php 
    $url = 'http://www.youtube.com/watch?v=9FOWI6Zftpg'; 
    $json = file_get_contents('http://www.youtube.com/oembed?url='.urlencode($url)); 
    $video = json_decode($json); 
?> 
<img src="<?php echo $video->thumbnail_url ?>" /> 
+0

這是我誤會......我怎麼搶JSON文件關閉FTP?我想我可以這樣做: 'http://www.youtube.com/oembed?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv% <?如果($ url){?><?php echo $ url; ?>' 但是,我會怎麼去拉縮圖並調用它? – FlannelBeard

+0

請參閱:http://stackoverflow.com/questions/15617512/get-json-object-from-url –

+0

找到[this](http://stackoverflow.com/questions/10377244/how-to-correctly-use - 從YouTube上插入拇指)以及...看着它。 – FlannelBeard