给最新文章添加一个New图标【修正代码】

Posted by 荒野无灯 @ 三月 2, 2010 at 11:18 下午 under wp技巧 24 Comments   

将以下代码添加到当前使用主题的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 字节
更新时间:2010年03月6日 累计下载:85 次
MD5 验证: [MD5验证工具下载]


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('@&lt;img(\s?)src=(.*?)(\s?)\/&gt;@','',$content);
        return $content;
}
add_filter('wp_list_pages','strip_page_icon_html',1000);

加上修正代码以后,一切OK了。

喜欢这篇文章吗?

请订阅本站 RSS feed

随机日志

Comments (24)

 

  1. 小风草堂 说:

    回去试试可以不。就像要这也的效果呢。
    比较喜欢你的站点,交换友情链接,不知意下如何,我先加了

Leave a Reply

XHTML: 留言可用标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



1,403 垃圾评论
截获自
Akismet

小提示:可按Ctrl+Enter快速提交 :mrgreen: