纯代码实现WordPress显示最近更新过的文章

有时候会因为一些功能的不完美或者代码的时效性、软件主题的更新等,对一些旧文章进行一些不定期更新,但是这种更新一旦没有及时通知访客,往往就没有什么意义了,访客很难知道。

1、代码部署

将以下代码添加到主题的 functions.php 里:

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 />&raquo; 修改时间: '  
               .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 天内发表的文章除外,具体使用的时候可以根据自己的情况修改这两个参数。作者有添加数据库缓存方式,所以在修改文章/删除文章/发表文章时才会更新缓存。
相关参数说明:$num - 展示数量,$days - 几天内的新文章除外。

http://xzh.i3geek.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途及非法用途,否则后果自负!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"qq301.com",如遇到无法解压的请联系管理员!
资源客是一个优秀的分享资源站,本站资源均为各位友友分享而来,特殊原创会标明如有侵犯版权等可联系删除

资源客 » 纯代码实现WordPress显示最近更新过的文章