來(lái)源于:
https://www.wpdaxue.com/wordpress-new-post-sign.html
給你新發(fā)布的文章(比如發(fā)布后24小時(shí)內(nèi))顯示一個(gè)【new】圖標(biāo),這樣可以提醒訪客。實(shí)現(xiàn)方法很簡(jiǎn)單,就是算個(gè)時(shí)間差,在規(guī)定時(shí)間內(nèi),插入特定文字或圖標(biāo)。
文字版顯示方法
在需要顯示的地方(比如標(biāo)題函數(shù)的后面)插入下面的代碼
我修改了一下:
$t1=$post->post_date;$t2=date("Y-m-d H:i:s");
$diff=(strtotime($t2)-strtotime($t1))/3600;
if($diff<24){
????echo '<div class="single-new">New</div>';
}
效果如下:

看到class 沒(méi)?自己來(lái)一點(diǎn)樣式,就很舒服。
換成圖片看看?
$t1=$post->post_date;$t2=date("Y-m-d H:i:s");
$diff=(strtotime($t2)-strtotime($t1))/3600;
if($diff<24){
????echo '<div class="single-new"><img src="https://oss.getimg.net/img/2019/05/24/1558713003.gif"/></div>';
????
}
什么效果?

還有
如果發(fā)現(xiàn) date()函數(shù)獲取的時(shí)間晚8個(gè)小時(shí),可以在上面的代碼的頂部添加
date_default_timezone_set('PRC');
用來(lái)定義默認(rèn)時(shí)區(qū)為中國(guó)時(shí)區(qū),感謝 @leon 的補(bǔ)充。
補(bǔ)充個(gè),來(lái)源于CU 主題:
效果如下:

在functions.php 中第一個(gè)<?php 下添加下列代碼:
/**最新文章數(shù)*/
function get_posts_count_from_last_24h($post_type ='post') {
global $wpdb;
$numposts = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(ID) ".
"FROM {$wpdb->posts} ".
"WHERE ".
"post_status='publish' ".
"AND post_type= %s ".
"AND post_date> %s",
$post_type, date('Y-m-d H:i:s', strtotime('-24 hours'))
)
);
return $numposts;
}
在需要顯示的文職添加下列代碼:
<!--最近更新-->
<?php if (get_posts_count_from_last_24h() != '0') { ?>
????????????<div class="zjgx">
?<?php echo get_posts_count_from_last_24h(); ?>
?</div>
????????<?php } else { } ?>
樣式:
/*最近更新*/
.zjgx{
????color: #fff;
????position: absolute;
????background: #f00;
????font-size: 12px;
????height: 18px;
????width: 18px;
????line-height: 18px;
????text-align: center;
????top: -9px;
????right: -9px;
????border-radius: 9px;
}
完