为博客文章添加 FriendFeed 和 Twitter 分享链接

在 functions.php 中添加函数 getShortenUrl,利用 bit.ly 生成博客文章链接的缩短地址:

function getShortenUrl() {
global $post;
$url = get_permalink($post->ID);

function getShortenUrl($postID) {
$url = get_permalink($postID);
$login = ‘user’;
$apiKey = ‘xxx’;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘http://api.bit.ly/shorten?version=2.0.1&longUrl=’ . urlencode($url) . ‘&login=’ . $login . ‘&apiKey=’ . $apiKey);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$result = curl_exec($ch);
curl_close($ch);
$result = @json_decode($result,true);
echo $result['results'][$url]['shortUrl'];
return $result['results'][$url]['shortUrl'];
}

抑制 the_ID 的输出:

<?php ob_start(); $ID = the_ID(); ob_end_clean(); ?>

调用 getShortenUrl,为博客文章添加 FriendFeed 分享链接:

<a href=”http://friendfeed.com/?url=<?php getShortenUrl(); ?>&title=<?php the_title(); ?> | Centeur’s Chaos” title=”Share on FriendFeed” target=”_blank”>FriendFeed</a>
<a title=”Share on FriendFeed” href=”http://friendfeed.com/?url=&lt;?php echo getShortenUrl($ID); ?&gt;&amp;title=&lt;?php the_title(); ?&gt; | Centeur’s Chaos” target=”_blank”>FriendFeed</a>

调用 getShortenUrl,为博客文章添加 Twitter 分享链接:

<a href=”http://twitter.com/home/?status=RT %40lypdarling: <?php the_title(); ?> | Centeur’s Chaos <?php getShortenUrl(); ?>” title=”ReTweet This” target=”_blank”>ReTweet</a>
<a href=”http://twitter.com/home/?status=RT %40lypdarling: <?php the_title(); ?> | Centeur’s Chaos <?php echo getShortenUrl($ID); ?>” title=”ReTweet This” target=”_blank”>ReTweet</a>

Update: 改了下是因为 getShortenUrl 一般都在 post 的 loop 之内,最后的 echo 去掉了是为了方便其它函数的调用。

Tags: , ,

This entry was posted on Sunday, May 31st, 2009 at 2:00 pm and is filed under wordpress. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Posts you may enjoy

6 Responses to “为博客文章添加 FriendFeed 和 Twitter 分享链接”

gravatar image

Zhu8 May 31st, 2009 at 8:19 pm | reply

这个Theme被你优化的挺漂亮的啊!

gravatar image

s May 31st, 2009 at 9:05 pm | reply

我觉得单片文章里面的google bookmarks分享链接不是很实用,如果改成share in google reader就更好了

gravatar image

Centeur May 31st, 2009 at 9:29 pm | reply

@Zhu8:原主题基本就这样,没怎么改,呵呵。

gravatar image

Zhu8 May 31st, 2009 at 9:30 pm | reply

@s: Sendoh?如果是你的话,那你真能神游啊!

gravatar image

Zhu8 May 31st, 2009 at 9:31 pm | reply

@Centeur:哈哈,我刚才还没有看到你的留言呢!嗯,我去他的网站看了。这个主题真不错。我喜欢深色的:)

gravatar image

s May 31st, 2009 at 9:31 pm | reply

@Zhu8: 我从你的twitter看到过来的呀

Leave a Reply

Please copy the string RU3Cw0 to the field below: