dtvx3420 2015-08-24 11:39
浏览 38

谷歌链接缩短与自定义社交媒体图标

I am looking to use Google Link shortener with Wordpress. I want to use shorter links to help users post more in social media, bitly is far to expensive. Google gives a massive allowance, which want to use. API key is available but need to implement using php prior Wordpress rendering the page.

Current Code

<a rel="nofollow" href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&amp;t=<?php echo urlencode(get_the_title($id)); ?>" title="Share this post on Facebook">
    <img src="<?php echo get_template_directory_uri(); ?>/images/facebook.png" border="0" alt="Facebook"/>
</a>
<a rel="nofollow" href="http://twitter.com/home?status=<?php echo urlencode("Currently reading: "); ?><?php the_permalink(); ?>" title="Share this article with your Twitter followers">
    <img src="<?php echo get_template_directory_uri(); ?>/images//twitter.png" border="0" alt="Twitter"/>
</a>
<a href="https://plus.google.com/share?url=<?php the_permalink(); ?>" title="Share this on Google+">
    <img src="<?php echo get_template_directory_uri(); ?>/images/google_plusone_share.png" border="0" alt="Google+"/>
</a>

To summarise I need a example of how to enforce Google link shortener API while using the in built functions such as:

the_permalink();

Many thanks.

</div>
  • 写回答

3条回答 默认 最新

  • dongshu9458 2015-08-24 11:56
    关注

    You dont want to hook into the_permalink, else every internal page link that uses it will be routed via google, which is obviously a bad idea.

    Instead create a new function to return the shortened link:

    //wrapper function to echo the result, consistent with Wordpress the_* and get_the_* style
    function the_google_short_link(){
        echo get_the_google_short_link();
    }
    
    function get_the_google_short_link(){
        // Google::shortlink is an example, replace with whatever code you have that creates the shortlink
        return Google::shortLink(get_permalink(get_the_ID())); 
    }
    

    then use in your templates are required:

    <a rel="nofollow" href="http://www.facebook.com/sharer.php?u=<?php the_google_short_link();?>&amp;t=<?php echo urlencode(get_the_title($id)); ?>" title="Share this post on Facebook">
        <img src="<?php echo get_template_directory_uri(); ?>/images/facebook.png" border="0" alt="Facebook"/>
    </a>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答