当前位置:首页 »“秋了秋”个人博客 » WP教程 » Wordpress调用近期编辑过的文章列表

Wordpress调用近期编辑过的文章列表

作者:秋了秋 发表时间:2015年10月26日

优秀网络文章应该是不断修改的,尤其是技术文章以及软件下载文章,很多东西都是需要按需更新,比如说下载链接,昨天发布的百度网盘外链今天就失效了,我们不得不更换链接,这就涉及到再次编辑文章,那如何让用户知道哪篇文章修改过呢?RT:不用插件给wordpress调用近期编辑过的文章

首先还是给wordpress的主题的function.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><i class="icon-cai"></i><a href="'.get_permalink().'" title="最终修改于'.get_the_modified_time('Y.m.d G:i').'">'
.$the_title_value.'</a></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>请拭目以待.</li>' : $recently_updated_posts;
echo $recently_updated_posts;
}

function clear_cache_zww() {
update_option('recently_updated_posts', ''); // 清空缓存
}
add_action('save_post', 'clear_cache_zww'); ///修改文章时触发清空缓存

然后在前端页面展示的地方添加调用代码:

<ul>
<?php if ( function_exists('recently_updated_posts') ) recently_updated_posts(7,35); ?>
</ul>

7表示调用七篇,35表示在35天之内编辑过的文章。这功能是带有缓存的,只有后台重新编辑文章之后才会清除缓存,这就节省了许多数据库查询工作,前台完全是静态展示。方法来自——zww

0
文章作者: “秋了秋”个人博客,本站鼓励原创。
转载请注明本文地址:http://netblog.cn/blog/403.html
目录: WP教程标签: 近期编辑 10543次阅读

请求播放音乐,请点击播放

登 录
点击获取验证码
还没账号?点击这里