Jump to content

Adding permalink to share icons


Sp0cK

Recommended Posts

Hi,

 

I've added social sharing icons to every article on the homepage. Now they just share the page itself - the idea is to share that (wordpress) post/article. I've succeeded to do so with facebook likes on the English version with adding 

<div class="fb-like" data-href="<?php the_permalink(); ?>"

Now I want to do the same with these 3 share icons:

<div class="social-content"><?php if (ICL_LANGUAGE_CODE=='zh-hans'): ?><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><?php endif; ?></div>

Can this be done by adding PHP? I'm not sure where I would have to add this...

 

Thanks

post-174304-0-93755300-1421887604_thumb.jpg

Link to comment
Share on other sites

I looked a bit more in the documentation of those sharing icons.
I'm pretty sure that if I can add this line of code there it will work 

<script type="text/javascript" charset="utf-8" >var bShareOpt = {url: "<?php the_permalink(); ?>"};</script>

So normally it's set to 

url: "";

 which directs to the page it's in. If I can add a piece of PHP code in javascript it will work, but I guess that's not allowed?
I tried to look up how to retrieve $post_id in wordpress by using javascript although I didn't come very far...

Ideas?

Edited by Sp0cK
Link to comment
Share on other sites

Is this a plugin, the posts loop or some random code you are sticking there?

 

If it's in the loop you can use get_the_ID()

<?php get_the_ID(); ?>

Can also make it a variable like this

<?php $postid = get_the_ID(); ?>

If the ID is not called within the_loop, then we can use the_ID rather than echo get_the_ID();

<?php the_ID(); ?>
Link to comment
Share on other sites

 

Is this a plugin, the posts loop or some random code you are sticking there?

 

If it's in the loop you can use get_the_ID()

<?php get_the_ID(); ?>

Can also make it a variable like this

<?php $postid = get_the_ID(); ?>

If the ID is not called within the_loop, then we can use the_ID rather than echo get_the_ID();

<?php the_ID(); ?>

 

It's from the documentation of bshare.cn which has all the share buttons for China. So I found out the permalink  

var bShareOpt = {url: "<?php the_permalink(); ?>"} 

and the_ID() actually do work. Problem is it shows the link/ID of the last article of that block instead the one I clicked on. See screenshot. article-share.jpg

 

Cheers!

Edited by Sp0cK
Link to comment
Share on other sites

If I use get_permalink(2343) it returns the correct post. So that works - meaning the $post_id is called from outside the loop.

https://tommcfarlin.com/get-permalink-outside-loop/
I read several documentations that something like:

function my_function() {
global $post;
get_permalink($post->ID);
} 

will make it so it can be called outside the loop.

So 

<script type="text/javascript" charset="utf-8" >var bShareOpt = {url: "<?php echo my_function(); ?>"};</script>

should work right? But it doesn't :)

Edited by Sp0cK
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.