WordPress添加显示最近更新过的文章功能

WordPress添加显示最近更新过的文章功能,主要适用于一些代码或者时效性要求比较高的文章,一旦更新则推到指定位置,让访问者更容易看到,按需添加。

  • 将以下代码添加到正在使用主题的 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'); // 新发表文章/修改文章时触发更新
  • 调用显示
<h3>Recently Updated Posts</h3> 
<ul> 
<?php if ( function_exists('recently_updated_posts') ) recently_updated_posts(8,15); ?> 
</ul>

参数解释:

recently_updated_posts(5,30) 内的 5 代表的是显示的文章数量,30是指的排除30天内发表的文章,按需修改。

《WordPress添加显示最近更新过的文章功能》版权归逐凨所有
转载请保留原文链接:https://izhizu.com/wordpress/98.html
THE END
分享
二维码
< <上一篇
下一篇>>