最近在做項目的時候遇到一個小需求,當文章內容有更新的時候需要有一個醒目的提醒。稍微思考了一下,WordPress 是有記錄更新時間的,只要對比發佈時間和更新時間,然後在做一個限定就可以完美實現了 WordPress 文章更新提示了。
把下面代碼加入到 functions.php 中,
global $post;
$punish_time = get_the_date('U');
$modified_time = get_the_modified_date('U');
$time = time();
if( ( $modified_time > $punish_time) && ( $time - $modified_time < 3600*24*7 ) )
return true;
}
functionis_modified(){ global$post; $punish_time=get_the_date('U'); $modified_time=get_the_modified_date('U'); $time=time(); if(($modified_time>$punish_time)&&($time-$modified_time<3600*24*7) ) returntrue; } |
然後在 loop 裏調用
<?phpif(is_modified()):?> 本文內容有更新 <?phpendif;?> |
如果有更新,則輸出你提示就可以了。