如题,我们有时候会因为一些功能的不完美或者代码的时效性、软件主题的更新等,对一些旧文章进行一些不定期更新,但是这种更新一旦没有及时通知访客,往往就没有什么意义了,访客很难知道。之前看过一篇文章《WordPress修改更新文章后邮件通知评论过那篇文章的用户》,觉得这个方法也很好,但是也有其局限性,比如邮件太多会造成垃圾邮件、有限的评论访客导致文章更新扩散不广等等。而这里提到的《WordPress纯代码实现展示最近更新过的文章》或许能有效的避免一些困扰吧,二者结合起来也可能更有效果。
1、代码部署
将以下代码添加到主题的 functions.php
里,代码来自ZWWoOoOo
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天内发表的文章除外,具体使用的时候可以根据自己的情况修改这两个参数。
CSS样式请自行根据自己主题来调整,这里就不给出CSS样式了。
评论前必须登录!
立即登录 注册