• Rob

    (@seomandarin)


    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)
  • 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

    (@seomandarin)

    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

    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.

    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

    (@seomandarin)

    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 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*

    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

    (@seomandarin)

    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.

    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

    (@seomandarin)

    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.