How To Show Last Updated date In WordPress

in This post, I will show you how to display the last update date or last modified date in your post or page, and it will work with any theme, including GeneratePress Premium, Astra, or any popular theme.

Here is a basic example of how you might approach this:

  1. Edit Your Theme’s functions.php File:
    • In your WordPress dashboard, go to “Appearance” and then “Theme Editor.”
    • Find and click on the functions.php file on the right sidebar.
  2. Add Custom PHP Code:
    • Add the following code at the end of your functions.php file:
// By TechZahi
add_filter( 'generate_post_date_output', function( $output, $time_string ) {
    $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on: %2$s</time>';

    if ( get_the_date() !== get_the_modified_date() ) {
        $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Last Updated on: %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 );

Keep in mind that this is a basic example, and you might need to customize it based on your specific requirements. Additionally, manually updating theme files carries the risk of causing issues if not done carefully, so make sure to have a backup before making any changes.

If you’re not comfortable with coding or want a more advanced and robust solution, using a dedicated plugin for generating and updating Last Updated date might be a safer and more efficient choice.

Leave a comment