Title: get_permalink post id outside loop
Last modified: August 22, 2016

---

# get_permalink post id outside loop

 *  [Rob](https://wordpress.org/support/users/seomandarin/)
 * (@seomandarin)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/get_permalink-post-id-outside-loop-1/)
 * Hi,
 * I’ve been struggling with this question for a while and altho there’s some topics
   regarding this subject I’m not sure how to implement it. It’s regarding social
   buttons on homepage sharingthe 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. If I add the post_id number `<?
   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
 * Using `get_queried_object_id` or `get_queried_object` is apparently another option
   to do so.
 * But unfortunately my PHP knowledge is very limited in order to glue the pieces
   together. I appreciate any help!

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [csloisel](https://wordpress.org/support/users/csloisel/)
 * (@csloisel)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/get_permalink-post-id-outside-loop-1/#post-5734503)
 * Would help to have more context about where you are trying to get the permalink.
   Are you on a single page? On an archive page? In the footer? In the sidebar?
 *  Thread Starter [Rob](https://wordpress.org/support/users/seomandarin/)
 * (@seomandarin)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/get_permalink-post-id-outside-loop-1/#post-5734504)
 * On the homepage at every article/post posted.. They are social sharing icons,
   when you click on one it adds the permalink(post_id) behind the link so the right
   article/post is shared. See image: [image](http://www.seomandarin.com/wp-content/uploads/article-share.jpg)
 * I do know why now, because it’s not in the loop 🙂 now im struggling with limited
   php knowledge to find a way to work around ths.
 *  [csloisel](https://wordpress.org/support/users/csloisel/)
 * (@csloisel)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/get_permalink-post-id-outside-loop-1/#post-5734508)
 * Why isn’t it in the loop? It seems appropriate to have it in the loop. When does
   it get printed on the page? Does it go through the whole loop before printing
   the icons? Are the social icons for each post all using the same permalink, or
   is there a constant offset?
 *  Thread Starter [Rob](https://wordpress.org/support/users/seomandarin/)
 * (@seomandarin)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/get_permalink-post-id-outside-loop-1/#post-5734536)
 * Permalink settings are: `website.com/%postname%.html`
 * Each social icon on the homepage should share that specific post. I did the the
   same on the English version of that website with Facebook Likes which works like
   a charm. Now i’m trying to achieve the same with the Chinese social icons…
 * At [loop.php](https://github.com/jiangzhouq/smart-mag/blob/master/loop.php) on
   line 28 I call the `get_permalink($postid)` function `<?php if (ICL_LANGUAGE_CODE
   =='zh-hans'): ?><script type="text/javascript" charset="utf-8" >var bShareOpt
   = {url: "<?php echo get_permalink($postid); ?>"};</script><div class="social-
   content"><div class="bshare-custom"><div class="bsPromo bsPromo2"></div><a title
   ="分享到微信" class="bshare-weixin" href="javascript:void(0);"></a><a title="
   分享到新浪微博" class="bshare-sinaminiblog" href="javascript:void(0);"></a><a
   title="分享到QQ空间" class="bshare-qzone"></a><a title="更多平台" class="bshare-
   more bshare-more-icon more-style-addthis"></a><span class="BSHARE_COUNT bshare-
   share-count" style="float: none;">19.5K</span></div></div><?php endif; ?>`
 * Most likely I’m doing something wrong *shame*
 *  [csloisel](https://wordpress.org/support/users/csloisel/)
 * (@csloisel)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/get_permalink-post-id-outside-loop-1/#post-5734537)
 * I’m confused. So it is _in_ the loop? If it is in the loop you could try removing
   the $postid arg from the function.
 *  Thread Starter [Rob](https://wordpress.org/support/users/seomandarin/)
 * (@seomandarin)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/get_permalink-post-id-outside-loop-1/#post-5734539)
 * I have tried. If I remove it, it will show the last article/post of that page.
 * So with $postid it will show the page it’s in, so basically doesn’t add any postid
   
   with just `echo get_permalink()’ it will return the last permalink/post of that
   page If I enter `echo get_permalink(2926)’ it will return only that postid, obviously.
 *  [csloisel](https://wordpress.org/support/users/csloisel/)
 * (@csloisel)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/get_permalink-post-id-outside-loop-1/#post-5734540)
 * From what I can tell the easiest and most logical solution would be to just put
   the code in the loop. You haven’t provided any reason why it has to be outside
   the loop.
 *  Thread Starter [Rob](https://wordpress.org/support/users/seomandarin/)
 * (@seomandarin)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/get_permalink-post-id-outside-loop-1/#post-5734541)
 * Where would I have to enter the code in order to put it in the loop? I apologize
   for asking that question 🙂

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘get_permalink post id outside loop’ is closed to new replies.

## Tags

 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [Post_ID](https://wordpress.org/support/topic-tag/post_id/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 8 replies
 * 2 participants
 * Last reply from: [Rob](https://wordpress.org/support/users/seomandarin/)
 * Last activity: [11 years, 4 months ago](https://wordpress.org/support/topic/get_permalink-post-id-outside-loop-1/#post-5734541)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
