首页 » 收集 » 正文内容
WordPress博客以纯代码实现展示最近更新过的文章
寻梦xunm| 685| 收集
3年前
超过1096天 温馨提示
本文最后更新于2021年09月20日,已超过1096天没有更新,若内容或图片失效,请留言反馈。

教程开始
本教程很简单 就不另外啰嗦了
我直接上教程,算是水了一篇文章哈哈哈
将以下代码添加到主题的 functions.php里
[hide]

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'); // 新发表文章/修改文章时触发更新

[/hide]
调用方法如下所示

<h3>Recently Updated Posts</h3>  
<ul>  
<?php if ( function_exists('recently_updated_posts') ) recently_updated_posts(8,15); ?>  
</ul>

代码中8为展示文章数量,15指15天内发表的文章除外

具体使用的时候可以根据自己的情况修改这两个参数。

CSS样式请自行根据自己主题来调整,这里就不给出CSS样式了

可以自行更改,我就不另外啰嗦了哈哈哈,感谢您的阅读

0 赞 or 打赏
喜欢就打赏一点
微信 支付宝
隐私
Q Q:1340326824
邮箱:vipshiyi@qq.com
QQ群:422720328

我的音乐

微博客-专为自己编写开发的源码