[英]Wordpress - get_permalink post id outside loop
I've been struggling with this question for a while and although there's some topics regarding this subject I'm not sure how to implement it. 我已经在这个问题上苦苦挣扎了一段时间,尽管有一些与此主题相关的主题,但我不确定如何实现。 It's regarding social buttons on homepage sharing the articles clicked on.
与首页上的社交按钮共享点击的文章有关。
I have to use 我必须用
<script type="text/javascript" charset="utf-8" >
var bShareOpt = {url: "<?php echo get_permalink(); ?>"};
</script>
which shows the permalink of the last ID instead of the link clicked on. 其中显示了上一个ID的固定链接,而不是单击的链接。 If I add the
post_id
number 如果我添加
post_id
号
<?php echo get_permalink(2926); ?>
it shows the correct post. 它显示正确的帖子。
Now this is because it happens outside the loop. 现在这是因为它发生在循环之外。
Other topics regarding getting post id outside the loop: get-a-posts-id wordpress-get-the-page-id-outside-the-loop accessing-post-id-outside-of-the-loop-for-listing-child-pages 有关在循环外获取帖子ID的其他主题:获取帖子ID WordPress获取页面ID之外的循环访问获取列表ID的外部循环的列表子页面
Using get_queried_object_id
or get_queried_object
is apparently another option to do so. 使用
get_queried_object_id
或get_queried_object
显然是另一种选择。
But unfortunately my PHP knowledge is very limited in order to glue the pieces together. 但是不幸的是,为了将各个部分粘合在一起,我的PHP知识非常有限。 I appreciate any help!
感谢您的帮助!
You'll need to get the ID of the page at the top of the template: 您需要在模板顶部获取页面的ID:
<?php $postid = get_the_ID(); ?>
Then in your javascript, presumably after the loop: 然后在您的javascript中,大概在循环之后:
var bShareOpt = {url: "<?php echo get_permalink($postid); ?>"}
Not sure how your code is laid out, but hope this helps! 不确定代码的布局方式,但希望对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.