Skip to content

Instantly share code, notes, and snippets.

@natejacobson
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natejacobson/63687b92b838b46b7814 to your computer and use it in GitHub Desktop.
Save natejacobson/63687b92b838b46b7814 to your computer and use it in GitHub Desktop.
Tag posts and pages with how recent they are in WordPress
// Assign to body on single pages and to each article in lists
add_filter( 'body_class', 'post_freshness_class' );
add_filter( 'post_class', 'post_freshness_class' );
function post_freshness_class( $classes ) {
$interval = ( current_time( 'Ymd', $gmt = 0 ) - get_the_date('Ymd') );
$interval_month = ( current_time( 'Ym', $gmt = 0 ) - get_the_date('Ym') );
if ( !is_page() ) {
if ( $interval <= 1 ) { $classes[] = 'past-day'; }
if ( $interval <= 7 ) { $classes[] = 'past-week'; }
if ( $interval_month < 1 ) { $classes[] = 'past-month'; }
if ( $interval_month <= 3 ) { $classes[] = 'past-quarter'; }
if ( $interval_month <= 6 ) { $classes[] = 'past-half'; }
if ( $interval <= 365 ) { $classes[] = 'past-year'; }
else { $classes[] = 'before-past-year'; }
}
return $classes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment