WordPress發(fā)布文章一般類型是置頂或者在默認正常。如果在24小時內最新發(fā)布的文章標題加上相應的圖標,不僅可以增加美觀,也可以提高用戶瀏覽網(wǎng)站的點擊率。實現(xiàn)方法很簡單,就是算個時間差,在規(guī)定時間內,插入特定文字或圖標。
網(wǎng)上有各種漂亮的圖標大家可以自己去搜一下。然后上傳網(wǎng)站替換下面代碼的圖片路徑即可。
方法一
function add_title_icon($title)
{
global $post;
$post_date=$post->post_date;
$current_time=current_time('timestamp');
$diff=($current_time-strtotime($post_date))/3600;
$title_icon_new=get_bloginfo('template_directory').'/images/new.gif';
if($diff<24){
$title='<img src="'.$title_icon_new.'" />'.$title;
}
return $title;
}add_filter('the_title','add_title_icon',999);
把以上代碼插入在主題文件夾的 functions.php 里就行了,可以修改代碼中的24為你想要的數(shù)值,則超過規(guī)定的時間后圖標就會自動消失。
用了以上代碼后,如果頁面列表里的鏈接也加上了和標題一樣的new圖標,可以添加以下代碼解決:
function strip_page_icon_html($content)
{
$content = preg_replace('@<img(\s?)src=(.*?)(\s?)\/>@','',$content);
$content = preg_replace('@<img(\s?)src=(.*?)(\s?)\/>@','',$content);
return $content;
}
add_filter('wp_list_pages','strip_page_icon_html',1000);方法二
$t1=$post->post_date;
$t2=date("Y-m-d H:i:s");
$diff=(strtotime($t2)-strtotime($t1))/3600;
if($diff<24){echo '<img src="'.get_bloginfo('template_directory').'/images/new.gif" alt='24小時內最新' />';} // new.gif 替換成你的圖片
else {echo "";} //時間超過時候顯示空白把這段代碼加到需要的地方就行。
本人強烈推薦使用方法二,如果發(fā)現(xiàn) date()函數(shù)獲取的時間晚8個小時,可以在上面的代碼的頂部添加以下代碼即可:
date_default_timezone_set('PRC'); //用來定義默認時區(qū)為中國時區(qū)





