How to display an icon when a new post is published and then remove it when a specific time past?

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.

Read More

How can i do this?

Related posts

1 comment

  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;
    }
    

Comments are closed.