Sp0cK Posted January 22, 2015 Share Posted January 22, 2015 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 Quote Link to comment Share on other sites More sharing options...
Sp0cK Posted January 23, 2015 Author Share Posted January 23, 2015 (edited) 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 January 23, 2015 by Sp0cK Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted January 23, 2015 Share Posted January 23, 2015 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(); ?> Quote Link to comment Share on other sites More sharing options...
Sp0cK Posted January 24, 2015 Author Share Posted January 24, 2015 (edited) 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. Cheers! Edited January 24, 2015 by Sp0cK Quote Link to comment Share on other sites More sharing options...
Sp0cK Posted January 25, 2015 Author Share Posted January 25, 2015 (edited) 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 January 25, 2015 by Sp0cK Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.