How can I use get_permalink() outside the loop?

I’ve been struggling with this question for a while and although there’s some information regarding this subject I’m not sure how to implement it. My question regards sharing the current page.

I am using <script type="text/javascript" charset="utf-8" >var bShareOpt = {url: "<?php echo get_permalink(); ?>"};</script> which shows the permalink of the last page instead of the current page. If I add the post_id number <?php echo get_permalink(2926); ?> it shows the correct post.

This is happening because it’s being called outside the loop.

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!

I like to add:
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 30 I call the get_permalink() function as such:

<?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="https://wordpress.stackexchange.com/questions/176647/javascript:void(0);"></a>
            <a title="分享到新浪微博" class="bshare-sinaminiblog" href="https://wordpress.stackexchange.com/questions/176647/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; ?>

2 Answers
2

Since you are calling get_permalink() outside of the loop, you need to define the post $id parameter.

You can access the post ID outside of the loop by invoking the global $post object and then referencing it’s ID value.

So your code would now look like this:

<?php if ( ICL_LANGUAGE_CODE=='zh-hans' ) : 

    global $post;
    $postid = $post->ID;

    ?>

    <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="https://wordpress.stackexchange.com/questions/176647/javascript:void(0);"></a>
            <a title="分享到新浪微博" class="bshare-sinaminiblog" href="https://wordpress.stackexchange.com/questions/176647/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; ?>

Leave a Comment