設(shè)置WordPress最新文章和置頂文章添加NEW和TOP小圖標(biāo)的方法

麥子 定制開(kāi)發(fā)251字?jǐn)?shù) 909閱讀3分1秒閱讀模式

我們?cè)诰庉嬀W(wǎng)站的時(shí)候,是不是希望如果我們給這篇文章設(shè)置置頂和最新文章的時(shí)候區(qū)別和其他文章的不同,這樣可以引起用戶(hù)的注意和點(diǎn)擊率。告訴用戶(hù)這篇文章是新更新的且置頂推薦的,那麥子看到有部分的網(wǎng)友是將置頂和新文章在文章標(biāo)題后面加上NEW和TOP圖標(biāo),這里我們需要先準(zhǔn)備圖標(biāo)圖片。

1、添加最新文章圖標(biāo)

//最新文章添加圖標(biāo)
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);

2、置頂文章圖標(biāo)

//置頂文章圖標(biāo)
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);

這里我們將代碼添加到當(dāng)前主題 Functions.php 文件中。當(dāng)然,我們需要準(zhǔn)備2個(gè)小圖標(biāo),分別是new.gif 和 top.gif,且放到對(duì)應(yīng)的主題目錄中。

投上你的一票
 
  • 本文由 麥子 發(fā)表于 2024年10月19日 10:09:04
  • 轉(zhuǎn)載請(qǐng)務(wù)必保留本文鏈接:http://bjj.org.cn/lasest-news.html
  • WordPress新圖標(biāo)