单击时我想打开一个弹出窗口,允许访问者通过 FACEBOOk 共享我的博客。一切正常,但问题是我无法获得当前文章的永久链接。如何在javascript中添加php?
// Replace the images with the following div and pin button
$button_div = '<div class="plugin">
<div class="imgshare">
<div class="postfb">
<a onclick="popItUp()">
<img src="http://www.example.com/wp-content/facebook.png" width="45px" />
</a>
</div>
</div>
<div class="demo"><img$1src="$2.$3" $4 width="$5" height="$6" /></div>
</div>';
// Replace the images with a containing div with a pin button on the image
$content = preg_replace( $pattern, $button_div, $content );
return $content;
}
}
<script>
function popItUp(url) {
var newWindow = window.open('http://www.facebook.com/share.php?u=<?php the_permalink(); ?>&title=<?php the_title(); ?>', 'name', 'height=615,width=475,scrollbars=yes');
if (newWindow.focus) {
newWindow.focus();
}
}
</script>
最佳答案
the_permalink()
仅在“循环”中有效。
我假设此代码在主帖子循环之外,在这种情况下,您需要使用 get_permalink()
并传递正在查看的帖子的 ID。
您可以通过从$wp_query
中获取post对象来获取当前post的ID;
function getPostID(){
$post = $wp_query->post;
return $post->ID;
}
现在您可以使用:
<?php echo get_permalink(getPostID()) ?>
关于javascript - 如何在 JS 文件中添加 wordpress 永久链接(onclick、window.open)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29348085/