"New" Badge for Recent Posts
Appends a "New" badge to post titles in list views for posts published within the last 7 days.
PHPby SnipCraft
php
<?php
add_filter( 'the_title', 'scseed_new_badge_on_recent_posts', 10, 2 );
if ( ! function_exists( 'scseed_new_badge_on_recent_posts' ) ) {
function scseed_new_badge_on_recent_posts( $title, $post_id ) {
if ( is_admin() ) {
return $title;
}
if ( ! is_archive() && ! is_home() && ! is_search() ) {
return $title;
}
$days_threshold = 7;
$post_date = get_post_field( 'post_date', $post_id );
$days_old = ( time() - strtotime( $post_date ) ) / DAY_IN_SECONDS;
if ( $days_old <= $days_threshold ) {
$title .= ' <span class="scseed-new-badge" aria-label="'
. esc_attr__( 'New', 'scseed' ) . '">'
. esc_html__( 'New', 'scseed' ) . '</span>';
}
return $title;
}
}#content#display#frontend#posts#ux