2012-05-21 39 views
93

我有一個數十或數百個帖子的頁面,每個帖子都帶有社交按鈕。 我只是不能生成每個網址的所有按鈕:它太慢(Facebook,g +,twitter,pinterest ...數百個鏈接)。因此,而不是Facebook共享按鈕在飛行中產生,我用一個簡單的IMG指向鏈接到「pin it」pinterest而不生成按鈕

https://www.facebook.com/sharer.php?u=${url_of_current_post}&t= 

當用戶點擊它,彈出窗口被Facebook生成的內容中打開。

我該怎麼做Pinterest的?我只能找到代碼來生成按鈕,但如果可能的話,我想盡量避免使用js。有沒有類似下面的內容?

http://pinterest.com/pinthis?url=${url_of_current_post} 

請不要試圖讓我使用js按鈕,謝謝。

回答

154

標準的Pinterest按鈕代碼(您可以generate here)是一個<a>標籤,它包裝Pinterest按鈕的<img>

如果您的頁面上沒有包含pinit.js腳本,則此標籤將按原樣工作。您可以通過在這些標籤上註冊您自己的點擊處理程序來改善體驗,從而打開一個具有適當尺寸的新窗口,或者至少在標籤中添加target="_blank"以使其在新窗口中打開點擊。

標籤語法看起來像:

<a href="http://pinterest.com/pin/create/button/?url={URI-encoded URL of the page to pin}&media={URI-encoded URL of the image to pin}&description={optional URI-encoded description}" class="pin-it-button" count-layout="horizontal"> 
    <img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /> 
</a> 

如果使用共享按鈕的JavaScript代碼,正在毀掉你的頁面加載時間,您可以通過使用異步加載方法來提高你的網站。有關使用Pinterest按鈕執行此操作的示例,請查看我的GitHub Pinterest button project with an improved HTML5 syntax

+3

很好的回答,謝謝!另請參閱我們的頁面developer.pinterest.com,在這裏:http://developers.pinterest.com/pin_it/ –

+0

Pin It widget構建器http://business.pinterest.com/widget-builder/#do_pin_it_button也是用於獲取一些示例代碼,然後可以通過編程方式進行自定義。 –

+6

@KentBrewster - 爲了什麼是值得的,我在訪問你的頁面後在同一個問題結束了。它擁有的信息都很棒,但它並沒有提到URL參數,也沒有提到URL可以在沒有javascript的情況下使用(在創建按鈕的情況下),就像facebook和twitter等效頁面一樣。 – xr280xr

2

我發現WordPress的一些代碼:

<script type="text/javascript"> 

    function insert_pinterest($content) { 
     global $post; 
     $posturl = urlencode(get_permalink()); //Get the post URL 
     $pinspan = '<span class="pinterest-button">'; 
    $pinurl = ''; 
    $pinend = '</span>'; 
     $pattern = '//i'; 
     $replacement = $pinspan.$pinurl.'$2.$3'.$pindescription.$pinfinish.''.$pinend; 
     $content = preg_replace($pattern, $replacement, $content); 
     //Fix the link problem 
     $newpattern = '/<span class="pinterest-button"><\/a><\/span><\/a>/i'; 
    $replacement = ''; 
    $content = preg_replace($newpattern, $replacement, $content); 
    return $content; 
    } 
    add_filter('the_content', 'insert_pinterest'); 

    </script> 

然後你把下面的在你的PHP:

<?php $pinterestimage = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full'); ?> 
<a href="http://pinterest.com/pin/create/button/?url=<?php echo urlencode(get_permalink($post->ID)); ?>&media=<?php echo $pinterestimage[0]; ?>&description=<?php the_title(); ?>">Pin It</a> 
1

所以,你想要的代碼到銷按鈕就可以了,而無需安裝按鈕?如果是這樣,只需將此代碼粘貼到您要從其固定的網頁的網址中即可。它應該作爲一個pin按鈕而沒有按鈕。

javascript:void((function(){var%20e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','http://assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());

+1

這對我有用,但有2個問題。你怎麼能保持彈出?另外,隨機數學是做什麼的? – Pat

+0

這是來自Pinterest的「書籤」的Pinterest官方代碼。它通常用作您保存到書籤的鏈接,並且可以點擊您訪問的任何網站以啓動Pinterest對話,但它也可以以這種方式正常工作,並且易於實施。 – ConorLuddy

+1

他們非常明確地告訴你不要在API文檔中這樣做。只因爲你可以,並不意味着你應該。 – Imperative

54

如果你想創建一個簡單的超鏈接,而不是銷它的按鈕,

更改此:

http://pinterest.com/pin/create/button/?url= 

要這樣:

http://pinterest.com/pin/create/link/?url= 

所以,一個完整的URL mi GHT只是看起來像這樣:

<a href="//pinterest.com/pin/create/link/?url=http%3A%2F%2Fwww.flickr.com%2Fphotos%2Fkentbrew%2F6851755809%2F&media=http%3A%2F%2Ffarm8.staticflickr.com%2F7027%2F6851755809_df5b2051c9_z.jpg&description=Next%20stop%3A%20Pinterest">Pin it</a>

+1

我更喜歡自己創建鏈接。這仍然有效,或者他們改變了嗎? – nouveau

+5

如果您有另一個pinterest按鈕(並加載pinit.js腳本),則接受的答案將生成一個按鈕。將url更改爲「鏈接」而不是「按鈕」將允許您在頁腳中有一個pinterest按鈕,並在頁面上的某個位置有一個自定義pinterest鏈接。這應該是被接受的答案。 – ashack

+1

Infact它是最好和準確的答案。謝啦。 –

5

我有同樣的問題。這在Wordpress中很有用!

<a href="//pinterest.com/pin/create/link/?url=<?php the_permalink();?>&amp;description=<?php the_title();?>">Pin this</a> 
0

爲使用小jQuery腳本

$('.linkPinIt').click(function(){ 
    var url = $(this).attr('href'); 
    var media = $(this).attr('data-image'); 
    var desc = $(this).attr('data-desc'); 
    window.open("//www.pinterest.com/pin/create/button/"+ 
    "?url="+url+ 
    "&media="+media+ 
    "&description="+desc,"_blank","top=0,right=0,width=750,height=320"); 
    return false; 
}); 

這將與linkPinIt類有圖像並存儲在HTML 5的數據說明所有環節的工作描述here您可以創建一個自定義鏈接屬性data-imagedata-desc

<a href="https%3A%2F%2Fwww.flickr.com%2Fphotos%2Fkentbrew%2F6851755809%2F" 
    data-image="https%3A%2F%2Fc4.staticflickr.com%2F8%2F7027%2F6851755809_df5b2051c9_b.jpg" 
    data-desc="Title for Pinterest Photo" class="linkPinIt"> 
    Pin it! 
</a> 

看到這個jfiddle example

4

對於這種情況,我發現Share Link Generator非常有用,它有助於創建Facebook,Google +,Twitter,Pinterest,LinkedIn分享按鈕。

相關問題