1

Every time i publish a new post i want to display an icon like a badge (NEW) and after a specific time it will disappear.

Lets say i publish a new post and i want to display that it is a new post. So i want to insert an icon that for example a star, in each new post in the blog list view into the metadata of the posts. Also i want to be removed after a specific time will pass. Lets say in one day or 24 hours the icon should be removed automatically.

How can i do this?

1 Answer 1

1

Filter the content of post_class():

add_filter( 'post_class', function( $classes ) {

    if ( is_singular() )
        return $classes;

    // now minus last mod time in seconds
    $diff = time() - mysql2date( 'U', $post->post_date );

    if ( DAY_IN_SECONDS <= $diff )
        $classes[] = 'new-post';

    return $classes;
});

Now, in your loop, use post_class(), and you get an extra class you can use in your stylesheet:

.new-post {
    padding-left: 20px;
    background-image: url(new.png) left top no-repeat;
}
18
  • i think that would work but how can i say how much time i want to keep the icon?
    – Charitos
    Commented Jun 9, 2013 at 19:35
  • @Charitos DAY_IN_SECONDS is the time difference. You can also use any number in seconds.
    – fuxia
    Commented Jun 9, 2013 at 19:40
  • You mean a day has 86400 seconds so i should write: if ( 86400 <= $diff ) Also help me a bit. Where do i have to insert the code? Into the functions.php or into the loop.php?
    – Charitos
    Commented Jun 9, 2013 at 19:44
  • Add the code to the functions.php. Template files like loop.php, header.php and so on, should never hold complex logic. Yes, you can use 86400 too.
    – fuxia
    Commented Jun 9, 2013 at 19:50
  • so in the loop i have only insert this code? <?php post_class(); ?> Do i have to add it to a css class or here $classes[] = 'new-post'; it is automatically take a css class?
    – Charitos
    Commented Jun 9, 2013 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.