"New" Badge on Recent Post Titles
Adds a small "New" label next to the titles of posts published in the last few days on your blog and archive pages.
Opens WPIDE on
Code
// Posts newer than this many days get the badge.
$wpide_new_badge_days = 7;
// Badge text.
$wpide_new_badge_text = 'New';
add_filter( 'the_title', function ( $title, $id = 0 ) use ( $wpide_new_badge_days, $wpide_new_badge_text ) {
if ( is_admin() || is_feed() || is_singular() || ! in_the_loop() || ! $id || 'post' !== get_post_type( $id ) ) {
return $title;
}
$published = (int) get_post_time( 'U', true, $id );
if ( $published && time() - $published < $wpide_new_badge_days * DAY_IN_SECONDS ) {
$title .= ' <span class="wpide-new-badge" style="display:inline-block;margin-left:.4em;padding:.1em .45em;font-size:.6em;vertical-align:middle;line-height:1.4;border-radius:3px;background:#d63638;color:#fff;">' . esc_html( $wpide_new_badge_text ) . '</span>';
}
return $title;
}, 10, 2 );
Notes
Change the number of days or the label text. Themes that use the title inside an HTML attribute will show the word as plain text there.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.