Adding an update status next to post titles in WordPress

This simple snippet adds an update status after your post title in WordPress.

It checks if the post has been updated since its creation and if this update is less than n days old. If the condition is verified, it adds an update status text after the title. In this example, the update interval is set to 7 days.

The update status text is styled with a nice colored label class. Check the documention to get more informations about the Twitter Bootstrap components available in the Customizr theme.

adding-an-update-status-to-posts-WordPress

 

Important : In the Customizr theme, use the ‘tc_content_headings’ filter instead of ‘the_title’ . 

 

add_filter('the_title' , 'add_update_status');
function add_update_status($html) {
	//First checks if we are in the loop and we are not displaying a page
	if ( ! in_the_loop() || is_page() )
		return $html;

	//Instantiates the different date objects
	$created = new DateTime( get_the_date('Y-m-d g:i:s') );
	$updated = new DateTime( get_the_modified_date('Y-m-d g:i:s') );
	$current = new DateTime( date('Y-m-d g:i:s') );

	//Creates the date_diff objects from dates
	$created_to_updated = date_diff($created , $updated);
	$updated_to_today = date_diff($updated, $current);

	//Checks if the post has been updated since its creation
	$has_been_updated = ( $created_to_updated -> s > 0 || $created_to_updated -> i > 0 ) ? true : false;

	//Checks if the last update is less than n days old. (replace n by your own value)
	$has_recent_update = ( $has_been_updated && $updated_to_today -> days < 7 ) ? true : false;

	//Adds HTML after the title
	$recent_update = $has_recent_update ? '<span class="label label-warning">Recently updated</span>' : '';

	//Returns the modified title
	return $html.'&nbsp;'.$recent_update;
}

  

Where to paste this code? => in your functions.php file. I strongly recommend to create a child theme. Download a start-up child theme here.

Everything you need to know about child theme with Customizr here.

35 thoughts on “Adding an update status next to post titles in WordPress”

Comments are closed.