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

廖为祥 wordpressWordPress调用近期编辑过的文章列表已关闭评论303阅读模式

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

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天之内编辑过的文章。这功能是带有缓存的,只有后台重新编辑文章之后才会清除缓存,这就节省了许多数据库查询工作,前台完全是静态展示。

继续阅读
廖为祥
  • 本文由 发表于 2022年12月13日 10:47:51
  • 除特别声明外,本站原创内容版权遵循 CC BY-NC-ND/2.5/CN协议规定