Why not giving a special style to the posts published less than 24 hours ago, to let your readers know about the fact they’re new? Here’s a simple code to do it.
Why not giving a special style to the posts published less than 24 hours ago, to let your readers know about the fact they’re new? Here’s a simple code to do it.
To achieve this recipe, edit your index.php file and look for the loop. Replace it with that one:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post();
$currentdate = date('Y-m-d',mktime(0,0,0,date('m'),date('d'),date('Y')));
$postdate = get_the_time('Y-m-d');
if ($postdate==$currentdate) {
echo '<div class="post new">';
} else {
echo '<div class="post">';
} ?>
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?></a>
<?php the_time('j F Y'); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
The above code will add the css class new if the post was published less than 24 hours ago. Then, you just have to modify your stylesheet a bit:
.post{
/* CSS style for "normal" posts */
}
.post.new {
/* CSS style for newer posts */
}
Thanks to Fred jaillet for the recipe!
21 Responses
Fine but what about posts less than 48 or 72 hours ago ?
@13770: It is possible and just a bit harder to do than this recipe. If people is interrested, I should write a new recipe to show how to make post younger than 48/72/whatever hours stands out
This is not as easy as it looks
@Raju: Explain your problem, I’ll try to help
Jean, I feel instead of a time based hack it would be better if it was hack that could highlight the latest post itself, so that it would work in any situation.
@Avinash: Very good idea! This can be done using a custom field or a “featured” category. I should write about it if peopel are interested
the latest post ?
No I’m not interested. If this post is 15 days old it’ll be highlighted all the time.
A current date :
//CURRENTDATE
$currentdate = date(‘Y-m-d’,mktime(0,0,0,date(‘m’),date(‘d’),date(‘Y’)));
@ Raju – Your right, it looks even easier
This is a great way to add a special flare to your current post.. Perhaps add some christmas lights around the title? Make a beige background? The choice is yours.
I don’t think I’d recommend it though for people that use full posts instead of post summaries.. A 1px border around the edge might get a little tedious after a while.
I used this way in my index.php to show only the first post different from the rest:
http://www.unintentionallyblank.co.uk/2007/02/18/adventures-with-the-wordpress-loop-sticky-articles/
This works independent from any time comparison, is very flexible and it doesn’t seem to make the loop any slower
Unless I’m crazy -> you missed <?php on line three:
<?php $currentdate = date(‘Y-m-d’,mktime(0,0,0,date(‘m’),date(‘d’),date(‘Y’)));
$postdate = get_the_time(‘Y-m-d’);
if ($postdate==$currentdate) {
echo ”;
} else {
echo ”;
} ?>
<a href=”" rel=”bookmark”>
Great idea!
@mydesignbytes: You’re right! Thanks for letting me know and sorry for the mistake!
Sorry, my code got scrambled
Learn to hack msn,aol,orkut,yahoo,myspace,etc
FOr author:Let us have a link exchange YOu put mine link in your blogroll and ill put yours
Well, how do you use this code, when you use WP 2.7s new post_class()-function?
@Ronny-Andre: WP 2.7 wasn’t released at the time I wrote this recipe. Sure, it is obsolete now.
Thanks for the quick reply! I found out how, maybe you could do a post about it?
http://curtishenson.com/new-wordpress-27-functions/
Trackbacks: