WordPress调用最近更新过的文章

时间:2019-08-29   阅读:469

老文章重新编辑修改以后,怕读者不知道?那就来给网站添加最新修改过的文章列表吧,这样一来,只要你更新了老文章,读者就可以很快知道了。如果你建的是软件站、电影站等分享的内容,那就不需要一个版本就发布一篇文章啦!给你的网站添加这个功能吧!

方法来自 zwwooooo 大师的 给老文章一个机会: Recently Updated Posts,都已经封装成函数了,加了2个参数:$num – 展示数量,$days – 几天内的新文章除外。还添加了数据库缓存方式,因为考虑到查询量,所以在你修改文章/删除文章/发表文章时才会更新缓存。

1. 把下面的函数代码扔到主题的 functions.php 的最后一个 ?> 的前面:

1
2
3
// Recently Updated Posts by zwwooooo | zww.mefunction 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'); // 新发表文章/修改文章时触发更新

// 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.可以使用下面的函数调用

1
2
3
4
<h3>Recently Updated Posts</h3>
<ul><?php if ( function_exists('recently_updated_posts') ) recently_updated_posts(8,15); ?></ul>

<h3>Recently Updated Posts</h3> <ul> <?php if ( function_exists('recently_updated_posts') ) recently_updated_posts(8,15); ?> </ul>

参数说明:8 为展示文章数量,15 指15天内发表的文章除外

具体细节和css样式,请自己修改,感谢 zwwooooo 大师!


上一篇:WordPress添加彩色标签云

下一篇:WordPress调用某段时间评论最多的文章

网友评论