Skip to content

Instantly share code, notes, and snippets.

@maor
Created July 25, 2013 12:20
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 maor/6079113 to your computer and use it in GitHub Desktop.
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.
<?php
/**
* @author Maor Chasen <maor@maorchasen.com>
*/
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