WordPress – New Post Label

I have recently implemented this code to create a new post label on my own site. Previously, I had just used a simple script to put a new label on the latest post. Now, the code checks the post’s date against the date two weeks ago. If it is less than that, then it displays the post.

I have used a tiny bit of CSS to position the new post label with z-index to bring it to the front. The CSS class for the label is “new-post”, which is for an empty div, with the label graphic set as the background image for the selector.

Here’s the code:

The PHP

<?php $post_date = the_date('Y-m-d','','',false); // Get the date of the post as a variable (not echo'd)
$twoweeks = strtotime("-2 weeks"); //The date from two weeks ago
$posted_date = strtotime($post_date); // The post date formatted to a comparable variable
if ($posted_date --> $twoweeks) {
$new_post_label = true; //If the post is less than two weeks ago, set the label to true.
} else {
$new_post_label = false; // Else, don't display the label.
}
?>
<ul>
<li><?php if($new_post_label == true){?>
<div class="new-post"></div>
<?php };?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="post-title">
<?php the_title(); ?></a>
<br />
<?php the_excerpt(); ?>
</li>
</ul>
<?php endwhile; else: ?>
<?php endif; ?>

 

The CSS


.new-post {
   position:absolute;
   right:0px;
   float:right;
   z-index:99;
   margin-top:0px;
   width:70px;
   height:70px;
   background:url('images/new-post.png') top right no-repeat;
}

And that’s it!

You can see the example of this code as implemented by my website.

Note: There’s probably a plugin for this, and probably an easier way, but this was easy enough to make part of my homepage template.

SHARE THE LOVE:Share on FacebookTweet about this on TwitterShare on Google+Pin on PinterestShare on LinkedInShare on TumblrShare on RedditEmail this to someone

You can use the spacebar or the arrow keys to navigate through my site. I hope you enjoy your journey.