2012-10-23 33 views
0

這是一個非常有趣的問題,我遇到了。我使用javaScript附加了一個pinterest按鈕的div,但是我使用Javascript內部的PHP來確定圖像的來源以及發送到pinterest的頁面的URL。評價php內部的javascript for wordpress博客的pinterest按鈕

我這樣做,而不是使用pinterest提供的純HTML,因爲我發現您可以使用自己的品牌pinterst按鈕.jpg而不是pinterest使用並強制您使用其品牌的iFrame。我已經在我的網站的其他部分成功地使用了這個javaScript,而不使用PHP。我對PHP和JavaScript非常陌生,所以我不確定我是否缺少任何東西,但我已經花費了無數小時來修補這個問題,並且需要一組新的眼睛。感謝你的幫助!

function add_social_media() { 
?> 
<p align="right"> 
    <a href="http://twitter.com/home?status=<?php print(urlencode(get_permalink())); ?>+<?php print(urlencode(the_title())); ?>"> 
<img src="/sites/aerialist.localhost/files/images/twitter.jpg" alt="Share On Twitter" /></a> 

<a href="http://pinterest.com/pin/create/button/?url=http%3A%2F%2Ffamousartistprints.com%2F&media=http%3A%2F%2Ffamousartistprints.com%2F" ><img border="0" src="/sites/aerialist.localhost/files/images/twitter.jpg"/></a> 

<script> 

jQuery(document).ready(function() { 

    jQuery('#pinItDiv').append('<a class="pin-it-button" href="http://pinterest.com/pin/create/button/?url='+ 
      <?php the_permalink(); ?>+ 
      '&media='+ 
      //set your image path here 
      <?php if(function_exists('the_post_thumbnail')) echo wp_get_attachment_url(get_post_thumbnail_id()); ?>+ 
      '&description='+<?php echo get_the_title(); ?>'">'+ 
      '<img class="pinItSuite" title="Pin It" src="/sites/aerialist.localhost/files/images/pinSuite.jpg" alt="" border="0" /></a>'); 

}); 

</script> 




<?php 
} 
add_action('thesis_hook_before_headline', 'add_social_media'); 
+0

我是盲人,否則我看不出這個問題本身什麼? ;) – roomcays

回答

0

據我瞭解,這是最好的做法,以保持PHP和JavaScript儘可能多的分離成爲可能。嘗試把你的JavaScript放在它自己的js文件中,並通過wp_enqueue_scripts與wordpress的腳本排隊。然後使用wp_localize_script將它需要的任何php變量(比如the_permalink()或get_the_title())傳遞給它。

查閱這些網站獲取更多信息:

http://wp.tutsplus.com/articles/how-to-include-javascript-and-css-in-your-wordpress-themes-and-plugins/

http://tosbourn.com/2011/08/wordpress/accessing-php-variables-from-within-javascript-under-wordpress/