Skip to content

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

2010 三月 2
by 荒野无灯

将以下代码添加到当前使用主题的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目录下面。
图标下载:


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填写您的邮件地址,订阅我们的精彩内容:,欢迎点击这里捐赠以支持荒野无灯转播到腾讯微博 转播到腾讯微博

作者:荒野无灯
出处:Hacklog【Hacklog】

声明: 本站遵循 署名-非商业性使用-相同方式共享 3.0 共享协议. 转载请注明转自Hacklog【荒野无灯weblog】

本文链接: http://ihacklog.com/?p=3484

32 Responses Post a comment

Trackbacks & Pingbacks

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <q cite=""> <strong>

 :wink:  :-|  :-x  :twisted:  :)  8-O  :(  :roll:  :-P  :oops:  :-o  :mrgreen:  :lol:  :idea:  :-D  :evil:  :cry:  8)  :arrow:  :-?  :?:  :!:

Note: You may use basic HTML in your comments. Your email address will not be published.

Subscribe to this comment feed via RSS