Site logo

Archived topic

Post Updated Date

9 replies · Started by Chandan on January 13, 2018

Viewing posts 1–10 of 10

Hello,

I need to show last updated date instead of published date. I did the following but there is some problem.

.posted-on .updated {
    display: inline-block;
}

.posted-on .updated:before {
    content: "Last updated: ";
}
time.entry-date.published {
    display: none;
}

Above shows updated for the post which was updated but blank which was not updated since published.

So I am looking for fixes which do the following.

Show Last updated when post was modified
Show original published date if post is not updated

This should also reflect in the source code. Currently, it shows published_time and modified_time both.

If you want to only show the modified date in the source if it's set, you can scrap that CSS and use this PHP:

add_filter( 'generate_post_date_output', 'tu_show_modified_date' );
function tu_show_modified_date() {
	$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
	
	if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
		$time_string = '<time class="updated" datetime="%3$s" itemprop="dateModified">%4$s</time>';
	}
	
	$time_string = sprintf( $time_string,
		esc_attr( get_the_date( 'c' ) ),
		esc_html( get_the_date() ),
		esc_attr( get_the_modified_date( 'c' ) ),
		esc_html( get_the_modified_date() )
	);

	printf( 
		'<span class="posted-on">%1$s</span>', // WPCS: XSS ok, sanitization ok.
		sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
			esc_url( get_permalink() ),
			esc_attr( get_the_time() ),
			$time_string
		)
	);
}

Let me know :)

Hello Tom,

It's not working as expected.

I removed those CSS and added what you mentioned in functions.php file of the child theme.

I still don't see last updated time on the blog post. Ex - https://geekflare.com/google-cloud-latency/ this still shows June but post was updated in November.

Source code still has both times.

<meta property="article:published_time" content="2017-06-15T22:04:32+08:00" />
<meta property="article:modified_time" content="2017-11-12T14:40:47+08:00" />

Please help. As mentioned earlier, I want to show only last updated time on a blog post and source code. However, if post is not updated then show the original published time.

I just adjusted the code a bit to test if it's working at all - can you update it and let me know?

I think I see the issue - I just adjusted the code above.

Hi there,

Instead of:

.posted-on .updated {
    display: block;
}

Try this:

.posted-on .updated {
    display: inline-block;
}

If this doesn't help can you open a new topic?

Thanks!

That worked, Thank you!

I did notice that the date that shows in WP Show posts and the rest of the site seems different. I will send an email into the support on that plugin since I have a pro version of that also.

No problem!

This archived topic is closed to new replies.