Halloween Sale 15% off Ends in See the plans
PHP

"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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.