Home > Technology, WordPress > 边侧栏实时显示twitter消息

边侧栏实时显示twitter消息

去年的时候介绍过用雅虎微管显示饭否的消息.我总是觉得它有点更新太慢了.毕竟通过了feed的中转.延时自然比较厉害.今天介绍个更好的办法.让它更新快一点. Twitter不过是个例子.其它的feed是一样的道理.

首先,我们要用到个函数.代码是从wp-includes->widgets.php中提取出来的.代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
function yinheli_widget_rss_output($rss, $args = array() ) {
if ( is_string( $rss ) ) {
require_once(ABSPATH . WPINC . '/rss.php');
if ( !$rss = fetch_rss($rss) )
return;
} elseif ( is_array($rss) && isset($rss['url']) ) {
require_once(ABSPATH . WPINC . '/rss.php');
$args = $rss;
if ( !$rss = fetch_rss($rss['url']) )
return;
} elseif ( !is_object($rss) ) {
return;
}
$default_args = array( 'show_author' => 0, 'show_date' => 0, 'show_summary' => 0 );
$args = wp_parse_args( $args, $default_args );
extract( $args, EXTR_SKIP );
 
$items = (int) $items;
if ( $items < 1 || 20 < $items )
$items = 10;
$show_summary  = (int) $show_summary;
$show_author   = (int) $show_author;
$show_date     = (int) $show_date;
if ( is_array( $rss->items ) && !empty( $rss->items ) ) {
$rss->items = array_slice($rss->items, 0, $items);
echo '<ul>';
foreach ( (array) $rss->items as $item ) {
while ( strstr($item['link'], 'http') != $item['link'] )
$item['link'] = substr($item['link'], 1);
$link = clean_url(strip_tags($item['link']));
$title = attribute_escape(strip_tags($item['title']));
$title =str_replace("yinheli: ","",$title);//将名字和后面的冒号去掉当然你也可以去掉前面的几个字符.函数是substr();
if ( empty($title) )
$title = __('Untitled');
$desc = '';
if ( isset( $item['description'] ) && is_string( $item['description'] ) )
$desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item['description'], ENT_QUOTES))));
elseif ( isset( $item['summary'] ) && is_string( $item['summary'] ) )
$desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item['summary'], ENT_QUOTES))));
if ( 360 < strlen( $desc ) )
$desc = wp_html_excerpt( $desc, 360 ) . ' [&hellip;]';
$summary = $desc;
if ( $show_summary ) {
$desc = '';
$summary = wp_specialchars( $summary );
$summary = "<div class='rssSummary'>$summary</div>";
} else {
$summary = '';
}
$date = '';
if ( $show_date ) {
if ( isset($item['pubdate']) )
$date = $item['pubdate'];
elseif ( isset($item['published']) )
$date = $item['published'];
if ( $date ) {
if ( $date_stamp = strtotime( $date ) )
$date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date_stamp ) . '</span>';
else
$date = '';
}
}
$author = '';
if ( $show_author ) {
if ( isset($item['dc']['creator']) )
$author = ' <cite>' . wp_specialchars( strip_tags( $item['dc']['creator'] ) ) . '</cite>';
elseif ( isset($item['author_name']) )
$author = ' <cite>' . wp_specialchars( strip_tags( $item['author_name'] ) ) . '</cite>';
}
if ( $link == '' ) {
echo "<li>$title{$date}{$summary}{$author}</li>";
} else {
echo "<li><a class='rsswidget' href='$link' title='$desc'>$title</a>{$date}{$summary}{$author}</li>";
}
}
echo '</ul>';
} else {
echo '<ul><li>' . __( 'An error has occurred; the feed is probably down. Try again later.' ) . '</li></ul>';
}
}
?>

把这个函数放到主题的functions.php中即可调用了.以上代码注意看我的注释.
调用的办法是:

<?php yinheli_widget_rss_output('http://twitter.com/statuses/user_timeline/17368142.rss',array('items'=>2, 'show_author'=>0 , 'show_date' =>false));?>

其中array(‘items’=>2, ‘show_author’=>0 , ‘show_date’ =>false)分别代表显示条目个数,是否显示作者,是否显示日期.(注意’=>’是数组的赋值.不要搞错了).

到这里基本完成,打开你的页面看看.消息显示出来了吗?嗯.去更新一下twitter在刷新页面,你是否发现,几分钟过去了.博客的调用还是没有更新!!!

这是因为wordpress自己有缓存.它用到了一个叫MagpieRSS的类.我们之前的函数就调用了那个来处理rss.要想做到比较实时的更新你还需要更改它的缓存周期.打开wp-includes->rss.php文件.找到大约第632行.修改其中的缓存时间.默认是1个小时.当然啦.你也可以完全关闭掉缓存,在624行,将其中的1修改为0就行了.

好了你在更新你的twitter.数据实时更新了吧…

  1. 吖Bee Mar 8th, 2009 @ 16:06 | #1

    哇咔咔~~沙发啊~philna1.05这个主题我在本地测试的时候,用IE7发现Comments有一点点问题~其他浏览器没事~

  2. yinheli Mar 8th, 2009 @ 16:17 | #2

    @吖Bee
    什么样的问题?貌似这主题用的人少.我都不打算经常更新了.除非有较大的bug

  3. 会律博客 Mar 8th, 2009 @ 18:10 | #3

    不错的方法,今天有幸刚去玩了下twitter,收藏住,可以试试看!

  4. aunsen Mar 8th, 2009 @ 18:45 | #4

    有空试下! :oops:

  5. yinheli Mar 8th, 2009 @ 19:42 | #5
  6. Lorz Mar 8th, 2009 @ 20:40 | #6

    哈,我 Twitter 是不公开的。用不到。

  7. Leeiio Mar 8th, 2009 @ 22:37 | #7

    以前都是用Twitter 官方的api來調用的哈,話說hack這個rss文件有啥後果麼?緩存時間為0會不會吃服務器~

  8. yinheli Mar 8th, 2009 @ 23:02 | #8

    @Leeiio
    测试中….

  9. Zoll Mar 9th, 2009 @ 03:13 | #9

    话说我正想弄一个来着~呵呵

  10. Zoll Mar 9th, 2009 @ 03:19 | #10

    想问一下能不能限制每条的字数,然后通过title来实现整条显示呢?如果一条显示太多会影像整体布局的~

  11. Leeiio Mar 9th, 2009 @ 10:41 | #11

    @Zoll
    应该是可以的哈我觉得~用php里面的截取函数,但是我不会~

  12. 达米安 Mar 9th, 2009 @ 13:54 | #12

    不用插件, 好啊! 我现在用的 twitter tool, 对于我来讲, 它的功能太强大了! 我都用不上!

  13. yinheli Mar 9th, 2009 @ 20:36 | #13

    @Zoll
    到我注释的那条下面添加一句:

    $title=wp_html_excerpt($title, 50);

    wp_html_excerpt是wordpress自带的函数(用于去掉html标签和截取指定长度文字),50表示截取的长度.注意:中文是三个字节,英文是一个字节.该函数不会四舍五入的. :cool:

  14. 任平生 Mar 19th, 2009 @ 18:08 | #14

    呵呵,可以试试用php 的 SimpleXML 来解析 xml ~~
    把你 Twitter rss 地址最后的.rss 换成 .xml

Submitting Comment, Give me a second...

Leave a comment

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
Trackbacks & Pingbacks ( 0 )
  1. No trackbacks yet.