Site logo

Archived topic

displaying last updated date & time on posts

7 replies · Started by dale on November 25, 2017

Viewing posts 1–8 of 8

Hi there.

On posts that I have updated after initially posting them, I want to display a "last updated" date in addition to the posting date. Initially I added this PHP via Code Snippets plugin, but it added the "last updated" text to pages as well as posts:

function wpb_last_updated_date( $content ) {
$u_time = get_the_time('U'); 
$u_modified_time = get_the_modified_time('U'); 
if ($u_modified_time >= $u_time + 86400) { 
$updated_date = get_the_modified_time('F jS, Y');
$updated_time = get_the_modified_time('h:i a'); 
$custom_content .= '<p class="last-updated">Last updated on '. $updated_date . ' at '. $updated_time .'</p>';  
} 
 
    $custom_content .= $content;
    return $custom_content;
}
add_filter( 'the_content', 'wpb_last_updated_date' );

Wp Beginner, at http://www.wpbeginner.com/wp-tutorials/display-the-last-updated-date-of-your-posts-in-wordpress/, suggests adding something like this to the theme, but obviouly it would need to be in a child theme and I'm not sure where to put it (or if it's correctly formatted for GP):

$u_time = get_the_time('U'); 
$u_modified_time = get_the_modified_time('U'); 
if ($u_modified_time >= $u_time + 86400) { 
echo "<p>Last modified on "; 
the_modified_time('F jS, Y'); 
echo " at "; 
the_modified_time(); 
echo "</p> "; } 

Thoughts? Thanks!

Hi there,

Try adding this CSS:

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

.posted-on .updated:before {
    content: "Last updated: ";
}

sorry, I'm not following. do you mean to use the CSS without any PHP, or to use it with one of the coding options I mentioned above?

Just that CSS above should do.

Have you given it a shot and not working?

Yes, I gave it a shot and didn't see any change. But I had Cloudflare on, so that might have been the issue. I just tried again (with CloudFlare in developer mode) and it worked!

Thanks! An easier solution than I expected :)

Glad I could help! :)

Hi there, I've tried adding this CSS and whilst it works, it's pushed hard up against the published date so it looks bad. Is there any way to just show the last updated date? When I try to remove the published date (display post date) it removes the Last Updated date too.
Thanks

This archived topic is closed to new replies.