WordPress調用最近更新過的文章

老文章重新編輯修改以後,怕讀者不知道?那就來給網站添加最新修改過的文章列表吧,這樣一來,隻要你更新瞭老文章,讀者就可以很快知道瞭。如果你建的是軟件站、電影站等分享的內容,那就不需要一個版本就發佈一篇文章啦!給你的網站添加這個功能吧!

方法來自 zwwooooo 大師的 給老文章一個機會: Recently Updated Posts,都已經封裝成函數瞭,加瞭2個參數:$num – 展示數量,$days – 幾天內的新文章除外。還添加瞭數據庫緩存方式,因為考慮到查詢量,所以在你修改文章/刪除文章/發表文章時才會更新緩存。

1. 把下面的函數代碼扔到主題的 functions.php 的最後一個 ?> 的前面:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Recently Updated Posts by zwwooooo | zww.me
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'); // 新發表文章/修改文章時觸發更新

// Recently Updated Posts by zwwooooo | zww.me
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’); // 新發表文章/修改文章時觸發更新

2.可以使用下面的函數調用

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

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

參數說明:8 為展示文章數量,15 指15天內發表的文章除外

具體細節和css樣式,請自己修改,感謝 zwwooooo 大師!

拓展閱讀:WordPress展示最近更新過的文章並通知評論過的用戶

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *