给最新文章添加一个New图标【修正代码】
2010 三月 2
将以下代码添加到当前使用主题的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 | //////start//////////title icon////////by 荒野无灯/////////////// 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/title_icon/new.gif'; $title_icon_top=get_bloginfo('template_directory').'/images/title_icon/top.gif'; $sticky = get_option('sticky_posts'); if($sticky) { $title=in_array($post->ID,$sticky)?'<img src="'.$title_icon_top.'" />'.$title:$title; } if($diff<24) { $title='<img src="'.$title_icon_new.'" />'.$title; } return $title; } //让标题显示icon add_filter('the_title','add_title_icon',999); //////end//////////title icon////////by 荒野无灯/////////////// |
再把 new.gif 和top.gif 两个图片文件上传到当前主题的images/title_icon目录下面。
图标下载:
title_icon.zip 下载 (508 字节, 160 次)
OK,这样就可以了。
如果要修改时间段,可以修改代码中的24为你想要的数值,则超过规定的时间后图标就会自动消失。
看一下效果吧:
前台:
后台:
2010年3月3日修正代码:
bug:用了以上代码后,页面列表里的链接也加上了和标题一样的new图标,如下图:
修正代码:
在原代码的基础上添加如下代码:
1 2 3 4 5 6 7 8 | //不能让页面也显示主题图标 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); |
加上修正代码以后,一切OK了。








Trackbacks & Pingbacks