给WordPress 新文章加个new(最新)标志

给你新发布的文章(比如发布后24小时内)显示一个【new】图标,这样可以提醒访客。实现方法很简单,就是算个时间差,在规定时间内,插入特定文字或图标。

效果实例见截图(嘻嘻,PS了一张)

new-post-sign-wpdaxue_com

文字版显示方法

在需要显示的地方(比如标题函数的后面)插入下面的代码

<?php 
$t1=$post->post_date;
$t2=date("Y-m-d H:i:s");
$diff=(strtotime($t2)-strtotime($t1))/3600;
if($diff<24){echo "<em>New</em>";} //这里就是显示的内容了
else{echo "";} //时间超过时候显示空白
?>

图片版显示方法

<?php 
$t1=$post->post_date;
$t2=date("Y-m-d H:i:s");
$diff=(strtotime($t2)-strtotime($t1))/3600;
if($diff<24){echo "<img src='new.gif' />";} // new.gif 替换成你的图片
else{echo "";} //时间超过时候显示空白
?>

new图标素材哪里找? 戳这里

2013-09-02 更新:

如果发现 date()函数获取的时间晚8个小时,可以在上面的代码的顶部添加

 date_default_timezone_set('PRC'); 

用来定义默认时区为中国时区,感谢 @leon 的补充。

来源:

https://www.wpdaxue.com/wordpress-new-post-sign.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32734.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?