WordPress

自定义wordpress分享按钮

LensNews

自定义wordpress分享按钮

不要再说用百度分享就可以,是可以,但我有我的想法和初衷,而说这话的人都没有看到!
远方的雪山建站一年多,刚开始用了JiaThis分享按钮,接触不久,就知道了百度分享,用百度分享一直到现在,所以对JiaThis也没有太多的感觉。

百度分享,为您带来更多流量!我信了,就用了。看重百度强大的中文搜索引擎,能给网站带来流量,但其实与其它分享按钮没有太多的区别,别人不分享,再强大的分享按钮放在那也是个摆设。

自定义分享按钮有几个初衷:

  • 1、分享按钮插件很强大,几乎包括所有的社交网站,但加载的链接多肯定会影响网速,测试了好多次也确是如此。
  • 2、不美观,分享按钮插件的样式不能网站本身的设计很好的融合在一起。
  • 3、能用代码实现的功能就用代码,尽量减少插件的使用。

替换分享链接中的标题与地址:

自定义分享按钮,其实是很简单,首页我们需要获取社交网站分享的链接地址,而这个不用我们操心,可以找百度分享、JiaThis等强大的分享按钮。在任何有百度分享或JiaThis分享按钮的网站中点击社交分享按钮都会弹出一个分享页面,网址就是我们需要的。

一般分享链接获取的都是当前页面的标题和地址,所以我们只需把分享链接中的标题与地址替换为wordpress的函数:
把分享链接中带有&t=、&title=等其它标题样式后面的标题替换为下面的代码:

  1. <?php the_title(''); ?>  

把分享链接中带有?url=、?link=等其它链接样式后面的地址替换为下面的代码:

  1. <?php the_permalink(); ?>  

因为分享链接中的标题与地址基本都是一大串字符,所以很难区分哪些才是标题,这里有个简单的方法,标题为例:

&title=%E5%88%86%E4%BA%AB%E6%8C%89%E9%92%AE&,在“&title=”后面与下一个“&”前面之间红色的字符就是标题了,地址也是这样。

添加分享链接按钮:

下面的代码为分享的链接地址,一些代码是网上有就没有重新编辑,把不需要的删除,需要的一些分享按钮萨龙龙重新编辑添加了进来。代码中的“社交图标链接地址”替换为你自己的图标链接地址,添加到single.php文件的相关位置:

  1. <!--远方的雪山自定义分享按钮-->   
  2. <div class="share"><h3>分享到:</h3>    
  3. <a target="_blank" rel="nofollow" href="http://twitter.com/home/?status=<?php the_title(''); ?>:<?php the_permalink(); ?>" class="twitter-share" title="Twitter"><img src="社交图标链接地址"></a>   
  4.   
  5. <a target="_blank" rel="nofollow" href="http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>&t=<?php the_title(''); ?>" class="facebook-share" title="Facebook"><img src="社交图标链接地址" /></a>   
  6.   
  7. <a target="_blank" rel="nofollow" href="http://www.douban.com/recommend/?url=<?php the_permalink(); ?>&title=<?php the_title(''); ?>" class="douban-share" title="豆瓣"><img src="社交图标链接地址"></a>   
  8.   
  9. <a target="_blank" rel="nofollow" href="http://www.diandian.com/share?lo=<?php the_permalink(); ?>&ti=<?php the_title(''); ?>&type=link" class="diandian-share" title="点点"><img src="社交图标链接地址"></a>   
  10.   
  11. <a target="_blank" rel="nofollow" href="http://t.sohu.com/third/post.jsp?&url=<?php the_permalink(); ?>&title=<?php the_title(''); ?>&content=UTF-8" class="sohu-share" title="搜狐微博"><img src="社交图标链接地址"></a>   
  12.   
  13. <a target="_blank" rel="nofollow" href="http://t.163.com/article/user/checkLogin.do?link=<?php the_permalink(); ?>&source=yfdxs.com&info=<?php the_title(''); ?> <?php the_permalink(); ?>" class="netease-share" title="网易微博"><img src="社交图标链接地址"></a>   
  14.   
  15. <a target="_blank" rel="nofollow" href="http://v.t.sina.com.cn/share/share.php?&url=<?php the_permalink(); ?>&title=<?php the_title(''); ?>" class="sina-share" title="新浪微博"><img src="社交图标链接地址"></a>   
  16.   
  17. <a target="_blank" rel="nofollow" href="http://v.t.qq.com/share/share.php?title=<?php the_title(''); ?>&url=<?php the_permalink(); ?>&site=https://yfdxs.com/" class="tencent-share" title="腾讯微博"><img src="社交图标链接地址"></a>   
  18.   
  19. <a target="_blank" rel="nofollow" href="http://www.kaixin001.com/rest/records.php?url=<?php the_permalink(); ?>&style=11&content=<?php the_title(''); ?>&stime=&sig=" class="kaixin-share" title="开心网"><img src="社交图标链接地址"></a>   
  20.   
  21. <a target="_blank" rel="nofollow" href="http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=<?php the_permalink(); ?>&title=<?php the_title(''); ?>&desc=&summary=&site=" class="qq-share" title="QQ空间"><img src="社交图标链接地址"></a>   
  22.   
  23. <a target="_blank" rel="nofollow" href="http://share.renren.com/share/buttonshare?link=<?php the_permalink(); ?>&title=<?php the_title(''); ?>" class="renren-share" title="人人网"><img src="社交图标链接地址"></a>   
  24.   
  25. <a target="_blank" rel="nofollow" href="http://shuqian.qq.com/post?from=3&title=<?php the_title(''); ?>&uri=<?php the_permalink(); ?>" class="qqsq-share" title="QQ书签"><img src="社交图标链接地址"></a>   
  26.   
  27. <a target="_blank" rel="nofollow" href="http://cang.baidu.com/do/add?iu=<?php the_permalink(); ?>&it=<?php the_title(''); ?>&linkid=hjm6y313aqz" class="baidu-share" title="百度云收藏"><img src="社交图标链接地址"></a>   
  28.   
  29. <a target="_blank" rel="nofollow" href="http://feed.feedsky.com/salonglong" class="rss-share" title="订阅我们"><img src="社交图标链接地址"></a>   
  30.   
  31. </div>  

或者也可以新一个php文件(custom_share.php),方便以后重复利用,把上面的代码添加到此文件中,再用下面的代码把custom_share.php包括进来:

  1. <!-- 分享 -->   
  2. <?php include('custom_share.php'); ?>   
  3. <!-- 分享end-->   

添加分享按钮样式:

分享按钮的样式,拷贝到主题的样式文件就OK:

  1. /*远方的雪山自定义分享样式*/  
  2. .share{    
  3.     background#f8f8f8;   
  4.     overflowhidden;   
  5.     height50px;   
  6.     border1px solid #ddd;   
  7.     border-radius: 5px;   
  8.     margin12px 0;   
  9. }   
  10. .user_avatar{   
  11.     background#f8f8f8;   
  12.     border1px solid #ddd;   
  13.     border-radius: 4px 4px 0 0;   
  14.     margin-bottom12px;   
  15. }   
  16. .user_avatar img{   
  17.     margin8px 6px 0 8px;   
  18. }   
  19. .share h3{   
  20.     font-size14px;   
  21.     floatleft;   
  22.     backgroundurl(images/share.png) #3275a8 no-repeat;   
  23.     height50px;   
  24.     width123px;   
  25.     text-indent: -999px;   
  26. }   
  27. .share img{    
  28.     padding: 0;   
  29.     bordernone;   
  30.     margin8px 1px;   
  31. }   
  32. .share h3,.share img{   
  33.     background-color#dedede;   
  34. }   
  35. .share img:hover,.share h3:hover{   
  36.     background-color#1e8dcc;   
  37. }  

效果可以看远方的雪山文章下面的分享按钮。

自定义分享按钮还是不错,设计成与远方的雪山一样的风格,看着就是舒服。
(20)

本文由 远方的雪山 作者:萨龙龙 发表,转载请注明来源!

LensNews

热评文章

评论:

25 条评论,访客:0 条,博主:0 条
  1. 加趣屋
    加趣屋发布于: 

    没看到你的站上有分享按钮啊

    • 萨龙龙
      萨龙龙发布于: 

      现在使用的是百度分享,而百度分享不支持 https 访问就关闭了。

  2. FlyHan
    FlyHan发布于: 

    你好,评论的样式是怎么修改的?搜了好多都不太好用

    • 萨龙龙
      萨龙龙发布于: 

      修改样式文件,一般是style.css文件,如果你不会CSS,那就没办法。

  3. 嘉诚
    嘉诚发布于: 

    主题很好!

  4. 同盟源
    同盟源发布于: 

    根据你的代码自己改了下,非常感谢

  5. 爱分享
    爱分享发布于: 

    貌似获取不了标题和网址?制作的分享按钮放在在wordpress侧边栏的。

    • 萨龙龙
      萨龙龙发布于: 

      刚每个都点了下,都没有问题

    • 萨龙龙
      萨龙龙发布于: 

      谢谢你的支持!我也很喜欢这个自定义的分享按钮!

  6. 艾因
    艾因发布于: 

    这个我参考了。。。给力

    • 萨龙龙
      萨龙龙发布于: 

      好啊,推荐使用。

  7. 番茄
    番茄发布于: 

    的确不错,很给力,我用的是百度分享。

    • 萨龙龙
      萨龙龙发布于: 

      之前了是用的百度分享,但能自定义的功能就不用插件。

  8. 沐歌
    沐歌发布于: 

    首字的样式挺好看的

    • 萨龙龙
      萨龙龙发布于: 

      参照WP logo的样式设置的短代码。

  9. 孙超博客
    孙超博客发布于: 

    谢谢博主的分享哈,一般弄个百度分享就O了

    • 萨龙龙
      萨龙龙发布于: 

      呵呵,百度分享中有太多社交按钮用不到,加载速度不是很快,设计成自己的风格不是很好吗?用WP我一直相信“能用代码实现的功能,就不用插件”。

  10. 百思不得姐
    百思不得姐发布于: 

    怎么你们的头像都是圆的,还可以转??

    • 萨龙龙
      萨龙龙发布于: 

      自定义了多说CSS样式。

  11. 间歇性歇斯底里的地盘
    间歇性歇斯底里的地盘发布于: 

    正在研究wordpress,博主很多东西都说的很详细。不错不错!看了博主网站感觉很多爱好相同或许能成为朋友哦。喜欢骑行,玩单反!呵呵~~

    • 萨龙龙
      萨龙龙发布于: 

      是啊,喜欢骑行,玩不起单反,只是爱好摄影,哈哈。等你的网站上线了,告诉我,关注关注。

发表回复