WordPress美化-最新文章和置顶文章添加NEW和TOP小图标的教程

添加最新文章图标

//最新文章添加图标
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);

置顶文章图标

//置顶文章图标
function add_top_title_icon($title)
{
    global $post;
    $title_icon_top=get_bloginfo('template_directory').'/images/top.gif';
    $sticky = get_option('sticky_posts');
    if($sticky)
    {
    $title=in_array($post->ID,$sticky)?'<img src="'.$title_icon_top.'" />'.$title:$title;
    }
    return $title;
}
add_filter('the_title','add_top_title_icon',999);

这里我们将代码添加到当前主题 Functions.php 文件中。当然,我们需要准备2个小图标,分别是new.gif 和 top.gif,可百度下载,且放到对应的主题目录中。

相关文章