去年的时候介绍过用雅虎微管显示饭否的消息.我总是觉得它有点更新太慢了.毕竟通过了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 ) . ' […]'; $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.数据实时更新了吧…
声明:本站遵循署名-非商业性使用-相同方式共享3.0共享协议. 转载请注明转自 PhilNa ™
哇咔咔~~沙发啊~philna1.05这个主题我在本地测试的时候,用IE7发现Comments有一点点问题~其他浏览器没事~