WordPress添加显示最近更新过的文章功能
本文最后更新于 632 天前,文中的信息可能已经有所变化。如有误,请留言反馈。

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天内发表的文章,按需修改。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇