Created
July 25, 2013 12:20
-
-
Save maor/6079113 to your computer and use it in GitHub Desktop.
Adds a new class ("new-post") if a post is fresh (within X days). For 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
<?php | |
/** | |
* @author Maor Chasen <[email protected]> | |
*/ | |
function mc_497346850334383( $classes, $class, $post_id ) { | |
$post = get_post( $post_id ); | |
if ( ! $post ) | |
return $classes; | |
$days_fresh = 5; // Posts that are older than (or equal to) N days won't be considered new | |
$post_date = new DateTime( mysql2date( 'Y-m-d', $post->post_date ) ); | |
$current_date = new DateTime( date( 'Y-m-d' ) ); | |
$diff = absint( $current_date->diff( $post_date )->format( '%a' ) ); | |
if ( $diff <= $days_fresh ) | |
$classes[] = 'new-post'; | |
return $classes; | |
} | |
add_filter( 'post_class', 'mc_497346850334383', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment