skip to Main Content

Add A Last Updated On Section to Blog Posts

Important: You no longer need to use custom code for this, the option is now available directly from the Customizer!

Once you add this code you can go to Appearance > Customize > Blog > Single and you will find the new block in the layout blocks at the bottom of the tab so you can move it around to where you want it. Before adding code like this you may want to instead create a Dynamic Template for your posts so you can design them exactly how you want via the WPBakery page builder.

/**
 * Adds a new "last_updated" block to the Blog post blocks that shows up in the Customizer.
 *
 * @link https://total.wpexplorer.com/docs/snippets/last-updated-block/
 */
add_filter( 'wpex_blog_single_blocks', function( $blocks ) {
	$blocks['last_updated'] = __( 'Last Updated', 'total' );
	return $blocks;
} );

/**
 *  Displays our custom "last_updated" block post block on the front-end.
 *
 * @link https://total.wpexplorer.com/docs/snippets/last-updated-block/
 */
add_filter( 'wpex_blog_single_layout_blocks', function( $blocks ) {
	if ( isset( $blocks['last_updated'] ) ) {
		$blocks['last_updated'] = function() {
			$original_time = get_the_time( 'U' );
			$modified_time = get_the_modified_time( 'U' );
			if ( $modified_time >= $original_time + 86400 ) {
				$updated_time = get_the_modified_time( 'h:i a' );
				$updated_day = get_the_modified_time( 'F jS, Y' );
				echo '<div class="last-modified">This post was last updated on ' . $updated_day . ' at ' . $updated_time . '</div>';
			}
		};
	}
	return $blocks;
} );
All PHP snippets should be added via child theme's functions.php file or via the Code Snippets Plugin (or alternative plugin)
Back To Top