Archived topic
How to add last modified date to posts?
9 replies · Started by Johanna on September 12, 2019
I want to display last modified dates and hide published date on my blog (without a plugin).
I tried Toms fixes:
https://generatepress.com/forums/topic/post-updated-date/#post-470725
https://generatepress.com/forums/topic/modified-post-date/#post-171970
And this specific PHP Code doesn't work:
add_action( 'generate_after_entry_header', 'tu_page_meta' );
function tu_page_meta() {
if ( is_singular( 'page' ) ) : ?>
<div class="entry-meta">
<?php generate_posted_on(); ?>
</div><!-- .entry-meta -->
<?php endif;
}
Hi there,
the first of those Two Links you provided should still work.
Where did you add that code?
And do you have any other code that is effecting the way the date/time is being displayed?
Hey David,
So I'm using a combination of Functions.php and CSS.
Here are the codes:
Functions.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
)
);
}
CSS:
.posted-on .updated {
display: inline-block;
}
time.entry-date.published {
display: none;
}
This worked to change the code on the page but doesn't seem to work in SERPs.
For instance, I look at my results in SERP for the article: https://www.dherealmark.com/grammarly-plagiarism-checker/ for query "Grammarly vs Turnitin" (I'm around pos 6) and it displays the published date and not the modified date.
Check with your SEO setup or any other plugin you're using for SEO Meta - currently you have published and update meta tags in the <head> of your site.
I currently use Yoast SEO do you think that could conflict?
And not sure about the meta tags but attached a screenshot of my tags: https://imgur.com/a/u71vv5K
Try this instead:
add_filter( 'generate_post_date_output', function( $output, $time_string ) {
$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="entry-date updated-date" 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() )
);
return sprintf( '<span class="posted-on">%s</span> ',
$time_string
);
}, 10, 2 );
No CSS needed :)
I pasted your PHP code into my functions.php file and removed the CSS code but I don't see any changes.
Tried it here: https://www.dherealmark.com/best-11-inch-laptop/
That article is on first page of Google but doesn't seem to have modified date in both SERP or post page.
Hmm, that code doesn't seem to be taking effect. Do you have any other generate_post_date_output filters?
Not that I know of.
Here's a screenshot of the placed snippet: https://imgur.com/a/fpiz4jC
And here's an ss of my plugins: https://imgur.com/a/HhAw2sq
Hi there,
what is the code directly above it - ending line 199? There shouldn't be open HTML in a functions.php file.