我的博客有的文章需要不定期的更新(如WordPress主题更新、游戏资源更新),但是更新后访客不知道,访客也总不能几十页几十页去翻查有木有更新,所以我在主题 zwwooooo 版本2 上线时在侧边栏加上:Recently Updated Posts 了,中文就是:最近更新过的文章
我觉得很多博客可能也需要这类功能,特别是那些不断更新的共享类文章,因为这类文章无需一个版本就发布一篇文章。
我写成函数了,加了2个参数:$num - 展示数量,$days - 几天内的新文章除外
另外我加了数据库缓存方式,因为考虑到查询量,所以在你修改文章/删除文章/发表文章时才会更新缓存。
下面是方法和代码
1. 把下面的函数代码扔到主题的 functions.php
// Recently Updated Posts by zwwooooo | zww.me function recently_updated_posts($num=10,$days=7) { if( !$recently_updated_posts = get_option('recently_updated_posts') ) { query_posts('post_status=publish&orderby=modified&posts_per_page=-1'); $i=0; while ( have_posts() && $i<$num ) : the_post(); if (current_time('timestamp') - get_the_time('U') > 60*60*24*$days) { $i++; $the_title_value=get_the_title(); $recently_updated_posts.='<li><a href="'.get_permalink().'" title="'.$the_title_value.'">' .$the_title_value.'</a><span class="updatetime"><br />» 修改时间: ' .get_the_modified_time('Y.m.d G:i').'</span></li>'; } endwhile; wp_reset_query(); if ( !empty($recently_updated_posts) ) update_option('recently_updated_posts', $recently_updated_posts); } $recently_updated_posts=($recently_updated_posts == '') ? '<li>None data.</li>' : $recently_updated_posts; echo $recently_updated_posts; } function clear_cache_zww() { update_option('recently_updated_posts', ''); // 清空 recently_updated_posts } add_action('save_post', 'clear_cache_zww'); // 新发表文章/修改文章时触发更新
2. 调用,如在侧边栏
<h3>Recently Updated Posts</h3> <ul> <?php if ( function_exists('recently_updated_posts') ) recently_updated_posts(8,15); ?> </ul>
参数说明:8 为展示文章数量,15 指15天内发表的文章除外
就这样,有需要的朋友可以试试。
zww
赏
oooo
or
- 本文标题:给老文章一个机会: Recently Updated Posts
- 本文链接:https://zww.me/archives/25487
- 发布时间:2011年07月27日 10:18
- 版权声明:除非注明,文章均为 zwwooooo 原创,转载请以链接形式标明本文地址!