Highlight newly published content by automatically displaying a “New” badge next to post titles for a specified number of days. This helps attract attention to fresh content and improves user engagement.
Steps
- Open your active theme’s
functions.phpfile or a custom functionality plugin. - Add the code below.
- Save the file.
- Newly published posts (within the last 7 days) will automatically display a New badge in their titles.
Benefits
- Highlights recently published posts.
- Increases visibility of fresh content.
- Encourages visitors to explore new articles.
- Easy to customize the badge duration.
- Lightweight solution with no plugin required.
/**
* Add a "New" badge to recently published posts.
*/
function wpfolks_new_post_badge( $title, $post_id ) {
if ( is_admin() || 'post' !== get_post_type( $post_id ) ) {
return $title;
}
$days = 7;
$published = get_post_time( 'U', true, $post_id );
if ( ( time() - $published ) < ( DAY_IN_SECONDS * $days ) ) {
$title .= ' <span class="wpfolks-new-badge" style="background:#d63638;color:#fff;padding:2px 6px;border-radius:3px;font-size:11px;margin-left:6px;">New</span>';
}
return $title;
}
add_filter( 'the_title', 'wpfolks_new_post_badge', 10, 2 );