WordPress: Show the date of the last post update

WordPress Show the date of the last post update

Good Morning! In this article, we are going to see how to get the date of the last update of an article to be displayed. Let’s go there!

1.  We access the functions.php file in the directory of our theme ( not recommended method ) or our snippet plugin  (recommended method).

2.  We create a snippet and paste the following code and save.

function bp_last_updated_date ($ content) {
    if (is_single ()) {
        $ 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 ('l, j F, Y');
                $ custom_content. = '<p class = "last-updated"> Updated on'. $ updated_date. '</p>'; }
        $ custom_content. = $ content;
        return $ custom_content;}

    else {
        return $ content;}}

add_filter ('the_content', 'bp_last_updated_date');

What this code will do is show an update date below the article publication date if the difference between them is at least 24 hours (86400 seconds). It also assigns the .last-updated  class to that date, so we can style it later.

3. Now if we go to an old article and update it (making any small changes for example), it will show us the date of the last update of the article. So we can show our users the freshest content

Leave a comment

Your email address will not be published. Required fields are marked *