【问题标题】:Code For Sharing Site Content on Social Networks在社交网络上共享网站内容的代码
【发布时间】:2014-02-18 16:25:52
【问题描述】:

我有一个网站,其博客页面上列出了文章,我希望为每篇文章分配一个自定义 Facebook 分享、推文和电子邮件链接。这是 Twitter 链接的代码:

<a href=\"https://twitter.com/share\" data-url=\"http://www.mywebsite.co.uk?article=$article_id\"><div class=\"shareCell shareTwitter\"></div></a>

问题是,当我点击推文图片时,它会尝试分享主博客页面而不是相关文章 - 任何想法为什么或如何对其进行排序。

另外,如何对 Facebook 和直接通过电子邮件发送文章做同样的事情?

【问题讨论】:

    标签: html facebook twitter social-networking


    【解决方案1】:

    你把整个html都转义了,我怀疑你用的是php。

    <a href=\"https://twitter.com/share\" data-url=\"http://www.mywebsite.co.uk?article=$article_id\"><div class=\"shareCell shareTwitter\"></div></a>
    

    应该是:

    <a href=\"https://twitter.com/share\" data-url=\"http://www.mywebsite.co.uk?article=" . $article_id . "\"><div class=\"shareCell shareTwitter\"></div></a>
    

    $article_id 是一个来自 php 的变量

    对于 facebook,这里有一个非常相似的方法:https://developers.facebook.com/docs/plugins/share-button/

    以及发送电子邮件:

    <a href="mailto:emailname@yourdomain.com?subject=encodeURIComponent('Check out the blogpost http://www.mywebsite.co.uk?article=" . $article_id . "');">Send in email</a>
    

    【讨论】:

    • 谢谢,但这仍然显示主页的网址 (mysite.co.uk/blog.php) 而不是文章 (mysite.co.uk/article.php?article=1)
    • 那么您的代码中似乎还有其他问题。因为如果 article.php 将您重定向到 blog.php,那么执行此操作的代码中必须有一些错误处理,或者 url 重写过滤器或类似的东西。
    【解决方案2】:

    <script>
    function fb_share(url)
    {
    var url = 'http://www.facebook.com/sharer.php?u=' +encodeURIComponent('<?php echo full_path();?>'+url);
    window.open(url, 'Share on FaceBook', 'left=20,top=20,width=550,height=400,toolbar=0,menubar=0,scrollbars=0,location=0,resizable=1');
    return false;
    }
    </script>
    //first upon put this given below php code in ur header or top of the page.
    
    <?php
    function full_path()
    {
        $s = &$_SERVER;
        $ssl = (!empty($s['HTTPS']) && $s['HTTPS'] == 'on') ? true:false;
        $sp = strtolower($s['SERVER_PROTOCOL']);
        $protocol = substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : '');
        $port = $s['SERVER_PORT'];
        $port = ((!$ssl && $port=='80') || ($ssl && $port=='443')) ? '' : ':'.$port;
        $host = isset($s['HTTP_X_FORWARDED_HOST']) ? $s['HTTP_X_FORWARDED_HOST'] : (isset($s['HTTP_HOST']) ? $s['HTTP_HOST'] : null);
        $host = isset($host) ? $host : $s['SERVER_NAME'] . $port;
        $uri = $protocol . '://' . $host . $s['REQUEST_URI'];
        $segments = explode('?', $uri, 2);
        $url = $segments[0];
        return $url;
    }
    
    ?>
    
    // This is the part of HTML Portion that you need to be included in your article section
    
    <a href="javascript:fb_share('?article=1')">Share this on Facebook</a>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      • 2011-03-28
      相关资源
      最近更新 更多