WordPress添加功能[1]-微博更新公告栏

还是在修改 Dot-A 这个新主题,打算把添加的几个效果,分别写成文章发出来,希望大家喜欢。

WordPress添加功能[1]-微博更新公告栏
WordPress添加功能[2]-待更新
WordPress添加功能[3]-待更新
WordPress添加功能[4]-待更新

废话少说,切入~~

侧边栏 轮转显示 Twi-t-ter更新 和 腾讯微博更新

说明:原理请参看 hzlzh 之前的两篇文章。网站调取Twi-t-ter信息  和 同步腾讯微博到Wordpress附缓存版

步骤

1、在 sidebar.php 中添加如下代码:

<div id="microblog">
<ul><li><div><img style="float: left; padding-right: 5px;float: left; padding-right: 5px;" alt="from Twi-t-ter" src="http://img.zlz.im/twitter.gif"><?php
function time_since($older_date, $newer_date = false)
{
$chunks = array(
array(60 * 60 , '小时'),
array(60 , '分钟'),
);
$newer_date = ($newer_date == false) ? (time()+(60*60*get_settings("gmt_offset"))) : $newer_date;
$since = $newer_date - $older_date;
if($since<86400 and $since>60)
{
for ($i = 0, $j = count($chunks); $i < $j; $i++)
{
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];
if (($count = floor($since / $seconds)) != 0)
{
break;
}
}
$output = "$count{$name}";
if ($i + 1 < $j)
{
$seconds2 = $chunks[$i + 1][0];
$name2 = $chunks[$i + 1][1];
if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0)
{
$output .= " $count2{$name2}";
}
}
return $output." 前";
}elseif($since<60 and $since=60){
echo '1 分钟 之内';
}else{
printf(the_time('Y-m-j G:i'));
}
}
?>
<?php
// Your Twi-t-ter username.
$username = "hzlzh";
$feed = "http://search.Twi-t-ter.com/search.atom?q=from:" . $username . "&rpp=1";
function parse_feed($feed) {
    $step1 = explode("<content type=\"html\">", $feed);
    $step2 = explode("</content>", $step1[1]);
    $tweet = $step2[0];
 
    $step3 = explode("<updated>",  $step2[1]);
    $step4 = explode("</updated>", $step3[1] );
    $time = $step4[0];
 
    $step5 = explode("<Twi-t-ter:source>",  $step4[1]);
    $step6 = explode("</Twi-t-ter:source>", $step5[1] );
    $from = $step6[0];
    $all = str_replace(array("&lt;","&gt;"),array("<",">"),array($tweet,$time,$from));
return $all;
}
$Twi-t-terFeed = file_get_contents($feed);
$result = parse_feed($Twi-t-terFeed);
list($tweet,$time,$from)=$result;
echo ''.strip_tags($tweet).'  <span style="font-style:italic;color:#959e87;">'.time_since(abs(strtotime($time)), time()).' From 推TE</span>';
//echo strip_tags($tweet).date("G:i:s A F-jS-Y",$time).strip_tags($from); //去掉注释,得到纯文本
?></div></li><li>
<?php
$id = 'hzlzh_com';
$url = 'http://Q.zlz.im/'.$id;
$e = ABSPATH . 'tx-microblog/'.$id.'.json';
$t = 3600; //缓存时间
$d = 'http://test.zlz.im/tx-microblog/default.json';
if ( !is_file($e) || (time() - filemtime($e)) > $t ){
copy($url, $e);//本地缓存
}
$jsonObject = @file_get_contents($e);
$decodedArray = json_decode($jsonObject, true);
echo ' <div><img style="float:left;
padding-right:5px;" alt="来自 腾讯微博" src="http://img.zlz.im/txwb.gif">'.$decodedArray[contents][0][content].'  <span style="font-style:italic;color:#959e87;">'.$decodedArray[contents][0][time].' 来自 '.$decodedArray[contents][0][from].'</span></div>';
?></li></ul>
</div>

2、之后在主题的 JS 文件(比如我放在:jquery.min.js 结尾)中添加如下代码控制轮播效果:

function AutoScroll(obj){
        jQuery(obj).find("ul:first").animate({
                marginTop:"-50px"
        },500,function(){
                jQuery(this).css({marginTop:"0px"}).find("li:first").appendTo(this);
        });
}
jQuery(document).ready(function(){
setInterval('AutoScroll("#microblog")',4000)
});
});

3、最后要设置 css样式(例如:style.css),否则轮播很难看是必然的:

#microblog{height:52px;
line-height:18px;
overflow:hidden;}
#microblog li{height:40px;
margin:10px 0;}

就这样吧,有问题 @hzlzh。

版权所有© HzlzH | 本文采用 BY-NC-SA 进行授权
转载需注明 转自: 《WordPress添加功能[1]-微博更新公告栏

相关文章

{ Leave a Reply ? }

  1. 小名 China Google Chrome Windows

    :oops: 沙发!
    期待“同步腾讯微博到WordPress[附 缓存版]”更新

  2. BoKeam China Google Chrome Windows

    期待其它功能,我不用国内的微博 :!:

  3. Ray Chow China Google Chrome Windows

    繼續求騰訊微博同步方法

  4. 静夜燃香 United Kingdom Mozilla Firefox Windows

    mark……我还挺想让我那推能滚动起来呢……

  5. 小邪 Netherlands Google Chrome Windows

    漂亮,嘿嘿,研究一下php的xml读取函数 ~

  6. mice China GreenBrowser Windows

    :roll: 如果附带一demo更好了..

  7. liheqi168 China SouGou Browser Windows

    微博又给人民带来福利、、

  8. 闲云野鹤 China Google Chrome Windows

    啥时候把那个腾讯的微博再折腾下一个版本吧.

  9. surda China 360 Browser Windows

    同步微博 好方法

  10. 万戈 China Mozilla Firefox Windows

    这项功能被你越来越完善了

  11. coolwinds China Google Chrome Windows

    :-o :-o :-o
    博主这个主题越来月耐看

  12. 七叶草 China SouGou Browser Windows

    看来我也搞个微博啊,不错的

  13. 柳亚 China Internet Explorer Windows

    希望它越来越强大,呵呵。。

  14. wmtimes China Mozilla Firefox Windows

    不错的想法,把两个较不错的信息动态显示。

  15. 闲云野鹤 China Google Chrome Windows

    我刚才想到了一个办法,不知道行不行,就是判断$url和$e的大小是否相等再更新,如果相同就不拷贝,不过不知道方法可行不可行.

  16. A.shun China Opera Windows

    先收藏。。
    话说我的微薄基本是废话,都不好意思同步了 :!:

  17. 点点 China SouGou Browser Windows

    貌似现在不行了啊?
    没更新?

  18. gooder8 China Internet Explorer Windows

    填填更健康 :arrow:

  19. kingchow China Mozilla Firefox Windows

    收藏了. 看看能用不. :-?

  20. 闲云野鹤 China Mozilla Firefox Windows

    $d是什么东西啊?期待版本更新啊 :-P

      • 闲云野鹤 China Google Chrome Windows

        @HzlzH: 不过我没有看见你上面写有判断失败的时候怎么载入$d啊…

  21. Rusaer China Opera Windows

    微博还是喜欢用digu的,简单一点,速度快一点,专业一点,尽管有点山寨

  22. 还好吧 呵呵

  23. 有点蓝 China Opera Windows

    又没折腾成功,败了 :evil:

  24. 海天 China Google Chrome Windows

    额 为什么我的不滚动啊
    腾讯微薄的也不出来

  25. 风吟 China Google Chrome Windows

    皓月闪烁,星光闪耀,中秋佳节,美满时刻!祝你全家幸福~快乐

  26. 小松 China Mozilla Firefox Windows

    你的腾讯微博弄得很不错~ :idea:

  27. 海天 China Google Chrome Windows

    额 怎么回事啊 貌似你的也不行啊~

  28. 求索阁 China 360 Browser Windows

    看的心痒痒的~博主能否给我发送一枚腾讯的微博注册码啊?

  29. 第六空间 Spain Google Chrome Windows

    还是用腾迅的微博吧,

  30. IM路人 China Maxthon Windows

    功能很完善了

  31. 补水面膜排行榜 China Mozilla Firefox Windows

    感谢博主的分享了,十分的牛 :-D

  32. yesureadmin China Google Chrome Windows

    有很多“来自”空白啊,难看啊

  33. 阿杰 China Internet Explorer Windows

    不错的创意设计啊

  34. 晓陆 China Mozilla Firefox Windows

    腾讯微博 widget不能用啊,显示如下,不能显示出文字

    Array ( [contents] => Array (
    [0] => Array (
    [content] =>
    [time] =>
    [from] =>
    )
    [1] => Array (
    [content] =>
    [time] => 11/25 20:03
    [from] => QQ)转播 点评
    )
    [2] => Array (
    [content] =>
    [time] => 11/25 19:28
    [from] => QQ)转播 点评
    )
    [3] => Array (
    [content] =>
    [time] => 11/25 19:19
    [from] => QQ)转播 点评
    )
    [4] => Array (
    [content] =>
    [time] => 11/25 19:02
    [from] => QQ)转播 点评
    )
    [5] => Array (
    [content] => 说腾讯不好,没门
    [time] => 11/08 09:15
    [from] => 网页)转播 点评
    )
    [6] => Array (
    [content] =>
    [time] => 10/26 19:09
    [from] => QQ)转播 点评
    )
    [7] => Array (
    [content] => 7f影响效率,分散注意力?
    [time] => 10/23 21:44
    [from] => QQ)转播 点评
    )
    [8] => Array (
    [content] =>
    [time] =>
    [from] =>
    )
    [9] => Array (
    [content] =>
    [time] =>
    [from] =>
    )

  35. 囧啊囧 China Mozilla Firefox Ubuntu Linux

    就是奔此文来的,现在用的你那个插件,自己去研究下这个。3Q

  36. mptr China GreenBrowser Windows

    收藏个先..留着以后用

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-D :-? :) :( :!: 8-O 8)

Pingback & Trackback

  1. 轮转显示微博更新 | Note:ongakuer United States WordPress Unknow Os - Pingback on 2010/10/07/ 09:02
  2. 给Wordpress添加上腾讯微博挂件或者插件 | 囧啊囧(vv15.com) United States WordPress Unknow Os - Pingback on 2010/12/09/ 23:42