0

This is a very interesting problem that I am running into. I am appending a div with a pinterest button using javaScript but I am using PHP inside the Javascript to determine what the source of the image and the URL of the page to send to pinterest.

I am doing it this way instead of using plain HTML provided by pinterest because I have found that you can use your own branded pinterst button .jpg instead of the iFrame that pinterest uses and forces you to use their branding. I have used this javaScript successfully on other parts of my sites without using PHP. I am very new to PHP and JavaScript so I am not sure if I am missing anything but I have spent countless hours tinkering with this problem and need a fresh set of eyes. Thanks for all your help!

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');
4

1 回答 1

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/

于 2013-05-20T08:06:55.877 回答