为博客文章添加 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=<?php echo getShortenUrl($ID); ?>&title=<?php the_title(); ?> | 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 去掉了是为了方便其它函数的调用。
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.



s May 31st, 2009 at 9:05 pm | reply
我觉得单片文章里面的google bookmarks分享链接不是很实用,如果改成share in google reader就更好了