Making sure your site visitors know when your posts were last updated is a great way of reassuring them of your content’s relevance. That’s why in today’s post I’m going to show you how to add a php snippet written by our sales/support agent Vlad Patrascu that alters your blog post’s date to show when the post was last updated.

Let’s get into it!

How to Show When Your Divi Blog Post Was Last Updated

Subscribe To Our Youtube Channel

I’m going to share a few different variations of this snippet so that you have a few options as to how your post meta data appears. All of these snippets require that you have a Divi child theme installed. Since we’ll be adding code to the functions.php file a child theme is needed so that future Divi updates do not override your changes.

Option 1: Swapping “Publish Date” for “Last Updated”

divi-blog-post-last-updated-1

This snippet makes it so that when a post is published it displays the “published” date, but when you make an update to that post it replaces the publish date with a “last updated” date as in the image above.

To implement this on your own website, you will need to navigate in your WP Admin to Appearance > Editor > functions.php.

Paste the following code snippet into your child theme’s functions.php file:

function et_last_modified_date_blog( $the_date ) {
	if ( 'post' === get_post_type() ) {
		$the_time = get_post_time( 'His' );
		$the_modified = get_post_modified_time( 'His' );

		$last_modified =  sprintf( __( 'Last updated %s', 'Divi' ), esc_html( get_post_modified_time( 'M j, Y' ) ) );
		$date = $the_modified !== $the_time ? $last_modified : get_post_time( 'M j, Y' );

		return $date;
	}
}
add_action( 'get_the_date', 'et_last_modified_date_blog' );
add_action( 'get_the_time', 'et_last_modified_date_blog' );

Click the blue “Update File” button to save. When you refresh your blog you should see the new “last updated” date on posts you’ve updated after publication.

Option 2: Adding the Specific Time to Option 1

divi-blog-post-last-updated-2

It is common on many news sites or anyone keeping up with current events to include the specific time an article was updated. Especially if a story is unfolding and visitors want to make sure their information is as up-to-date as possible.

To add the time to the snippet above, simply locate the following line of code:

get_post_modified_time( 'M j, Y' ) ) );

And replace it with this line of code:

get_post_modified_time( 'M j, Y at g:iA' ) ) );

When finished click the blue “Update File” button to save.

Option 3: Displaying Both the Publish and Last Updated Dates (With Time)

divi-blog-post-last-updated-3

If you would like to display both the publish date and the last updated date then use this snippet:

function et_last_modified_date_blog( $the_date ) {
	if ( 'post' === get_post_type() ) {
		$the_time = get_post_time( 'His' );
        $the_modified = get_post_modified_time( 'His' );

		$last_modified =  sprintf( __( 'Last updated %s', 'Divi' ), esc_html( get_post_modified_time( 'M j, Y at g:iA' ) ) );
		$published =  sprintf( __( 'Published on %s', 'Divi' ), esc_html( get_post_time( 'M j, Y' ) ) );

		$date = $the_modified !== $the_time ? $last_modified . ' | ' .  $published : $published;

		return $date;
	}
}
add_action( 'get_the_date', 'et_last_modified_date_blog' );
add_action( 'get_the_time', 'et_last_modified_date_blog' );

When finished click the blue “Update File” button to save.

Option 4: Displaying Both the Publish and Last Updated Dates (Without Time)

divi-blog-post-last-updated-4

To remove the specific time from this option, simply reverse the directions for Option 2.

Replace this line of code:

get_post_modified_time( 'M j, Y at g:iA' ) ) );

With this line of code:

get_post_modified_time( 'M j, Y' ) ) );

When finished click the blue “Update File” button to save.

Wrapping Up

While this edit is a simple one it can be extremely valuable for content creators. If you’re a Divi designer or developer it’s a great feature to add to your starter child theme so that if a client wants the feature you have it on hand.

What other Divi tutorials would you like to see? Let us know in the comments below!


LEAVE A REPLY

Please enter your comment!
Please enter your name here