2

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="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; ?>
9
  • Can you include the rest of your code?
    – jetlej
    Jan 31, 2015 at 2:01
  • Included. Let me know if you need more.
    – Sp0cK
    Jan 31, 2015 at 3:21
  • 1
    Why is this occurring outside the loop? Why not just do it inside?
    – mor7ifer
    Jan 31, 2015 at 4:08
  • Welcome to the WordPress Development community, @Sp0cK! I am still somewhat confused as to your desired outcome, and I must second @m0r7if3r's question: the posted code segment displays post-relevant data which is the very reason that The Loop exists - why do it externally? You mention that the posted code segment is in your theme's loop.php file, however the loop.php file in the linked GitHub repository does not contain the segment. Additionally, within the repository, the referenced line #28 appears to be inside The Loop. Where are you trying to use the posted code segment?
    – bosco
    Jan 31, 2015 at 5:17
  • If you say Line #28 is inside the loop. Then I'm confused why it doesn't return the correct $post_id. I'm trying to use the posted code segment in every article on the homepage. See image the social icons appear with mouse hover
    – Sp0cK
    Jan 31, 2015 at 6:02

2 Answers 2

1

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="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; ?>
2
  • It returns the last $postid - meaning it might still not be in the loop
    – Sp0cK
    Feb 3, 2015 at 0:48
  • Hey Sp0ck, in order for me to better help you I need to see the whole code. The link you posted to github doesn't include the code you shared here...
    – Adal
    Feb 4, 2015 at 2:04
0

The unique way I sort this out has been using PHP (not wordpress functions):

<?php if($_SERVER[ 'REQUEST_URI' ] == "/my-url/"){ ?>
   Do something if the user is in the "my-url" page
<?php } ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.