Last active
August 29, 2015 14:17
-
-
Save natejacobson/63687b92b838b46b7814 to your computer and use it in GitHub Desktop.
Tag posts and pages with how recent they are in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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