Code for updated post date stamp

Evening,

Can you help assist with a date stamp for updated posts (or modified date). I think this should go in php.file or CSS?

Thanks
Gemma

Hello @twoscotsabroad,

Thanks for writing in! :slight_smile:

Please download and install the child theme of your theme: How To Setup Child Themes

Then to achieve this, please follow the steps below:

  1. Copy the functions.php from your theme to your child theme.
    E.g. root/wordpress/wp-content/themes/Your-theme to root/wordpress/wp-content/themes/Your-child-theme

  2. Then add this code into the functions.php in your child theme.

function display_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>'.Last updated on '. $updated_date . ' at '. $updated_time .'</p>';  
} 
 
    $custom_content .= $content;
    return $custom_content;
}
add_filter( 'the_content', 'display_last_updated_date' );

This code checks to see if a post’s published date and last modified dates are different. If they are, then it displays last modified date before the post content.

Hope it helps.

Thank you!

It’s showing on all pages like homepage, about met etc - could you provide coding that removes this please?

Sorry!

Hi there,

If you want to avoid showing this in some pages you need to use the template tags of the WordPress to exclude the code for that pages. For example if you want to remove for the homepage the code should be:

function display_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'); 
    
    if (!is_home()) {
		$custom_content .= '<p>'. 'Last updated on '. $updated_date . ' at '. $updated_time .'</p>';  
    }
    
    $custom_content .= $content;

    return $custom_content;
}
add_filter( 'the_content', 'display_last_updated_date' );

Thank you.

Thank you.

Can you provide the final coding for me to add please - the updated date should not appear on any pages, only posts.

Hi there,

If you want to have it only for the posts you need to use the is_single() so the code will be:

function display_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'); 
    
    if (is_single()) {
		$custom_content .= '<p>'. 'Last updated on '. $updated_date . ' at '. $updated_time .'</p>';  
    }
    
    $custom_content .= $content;

    return $custom_content;
}
add_filter( 'the_content', 'display_last_updated_date' );

Thank you.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.