Add a new post "NEW" flag to your WordPress theme

888u

Last update at :2023-12-31,Edit by888u

Many wordpress themes have integrated the "NEW" mark for newly added articles. Some people like this and some don't. If the wordpress theme you use does not have such a function, you can still add it yourself. The advantage is that regular visitors can find the latest content of the website at a glance, hehe. Here, this site has collected the latest tips for everyone, and the method is very simple. All we need to do is put the code provided below where you want it to be displayed, such as after the article title of the homepage list (mostly in the middle of

), or in the gadget article list, etc. You need to add it to the corresponding file.

Text prompt code

The simplest way is to directly output an English word NEW

  1. $t1=$post->post_date;
  2. $t2=date(“Y-m-d H:i:s”);
  3. $diff=(strtotime($t2)-strtotime($t1))/3600;
  4. if($diff<24){echo “New”;} //The number is the number of hours to judge the output NEW, please modify it according to your own situation
  5. else{echo "";} //No content will be output if the time exceeds
  6. ?>

Picture version prompt code

To make it more beautiful, we can make a separate reminder image.

  1. $t1=$post->post_date;
  2. $t2=date(“Y-m-d H:i:s”);
  3. $diff=(strtotime($t2)-strtotime($t1))/3600;
  4. if($diff<24){echo "Add a new post ";} //The number is the number of hours to judge the output NEW, please modify it according to your own situation
  5. else{echo "";} //No content will be output if the time exceeds
  6. ?>

Beautified version

Compared with the image version, which directly outputs images, I chose to set the prompt image in CSS, which can better control the display position of the prompt image and match the corresponding image to achieve better visual effects. In the upper right corner of the title, there is a suitable subtitle without any sense of violation.

html code, placed in front of the home page article list title

  1. $t1=$post->post_date;
  2. $t2=date(“Y-m-d H:i:s”);
  3. $diff=(strtotime($t2)-strtotime($t1))/3600;
  4. if($diff<24){echo "";} //The number is to determine the number of hours to output NEW, please base it on your own situation Modify
  5. else{echo "";} //No content will be output if the time exceeds
  6. ?>

The css code is written into style.css

  1. .new–post {
  2. background: url(img/new.png) no–repeat;
  3. position: absolute;
  4. width: 44px;
  5. height: 45px;
  6. right: –3px;
  7. top: –3px;
  8. z–index: 10;
  9. }

Please back up the file when modifying it to prevent timely recovery in the event of an error. In fact, most of the time you just mess around on your own, so take your time! In addition, it does not mean that the website must add these small functions. The more functions, the more resources it takes up. A host with too many restrictions may cause various problems, so please choose reasonably according to your own needs.

Recommended site searches: Telecom IP proxy, US virtual space, domain name registration service website, Wanwang space, permanent free cloud server address, free international domain name registration space, dynamic IP dialer, US host network, US multi-IP station group vps,

Add a new post

All copyrights belong to 888u unless special state
取消
微信二维码
微信二维码
支付宝二维码