我们有时候发布的文章并没有完善,或者后期某个教程会不断完善,所以希望在网站侧边栏显示出来.
毕竟既然坚持更新就是希望能够被更多的关注,才会更有动力.
而WordPress主题开发调用的新发布的文章列表并不能实现这一功能.
下面来示范下通过代码实现侧边栏调用近期更新文章,当然你也可以放在其他位置.
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_zuijin() { update_option('recently_updated_posts', ''); // 清空 recently_updated_posts } add_action('save_post', 'clear_cache_zuijin'); // 新发表文章/修改文章时触发更新
上面的$num=10,$days=7里面的10与7代表获取数量10篇,7天内更新的文章.
仅仅展示如何调用,样式请自行修改
<h3>近期更新的文章列表</h3> <ul> <?php if ( function_exists('recently_updated_posts') ) recently_updated_posts(8,15); ?> </ul>