Javascript在循环之外,因此php永远无法访问get_the_title()
, get_permalink()
和get_the_post_thumbnail()
从那里开始。
我要做的是在循环中的某个地方包括这些内容,例如:
<div class="entry-content">
<h2 class="post-title"><?php the_title(); ?></h2>
<a class="share-button" data-title="<?php the_title(); ?>"></a>
</div>
然后使用jQuery,您可以从循环生成的HTML中获取它们。例如,以下内容(对链接和图片也一样):
<script type="text/javascript">
$(document).ready(function(){
$(\'#share_button\').click(function(e){
e.preventDefault();
var postTitle = $(this).data( "title" ); // Here\'s where you grab the title
FB.ui({
method: \'feed\',
name: postTitle,
link: \' <?php echo get_permalink(); ?>\',
picture: \'<?php echo get_the_post_thumbnail();?>\',
caption: \'This is the content of the "caption" field.\',
description: \'This is the content of the "description" field, below the caption.\',
message: \'\'
});
});
});
</script>