Home WordPress 2 Ways to Display the Last Modified Date of Posts in WordPress

2 Ways to Display the Last Modified Date of Posts in WordPress

If you have a WordPress-based static site, you might want to regularly update your articles to keep them relevant. And to let your visitors know that the article they are reading keeps updated, you need to display when did the article last updated.

There are lots of ways to display the last modified date of your WordPress posts. The easiest one maybe is by using a plugin, but this method doesn’t work on some themes. There is another way with a higher possibility it will work: by editing your theme.

Which theme part need to edit?

First, you can edit a code that is responsible for displaying the post meta (date, author, category). Alternatively, you can also add a new function to the functions.php file on your theme.

1. Editing the code

Let’s start with the first method, by editing the code that is responsible for displaying the post meta.

The code that is responsible for displaying the post meta can be different on each WordPress theme. In most cases, you can find the code lines on the single.php file, but not always. For instance, on most themes developed by Flexithemes (including Vacation Time), you can find the code lines on the post-meta.php file. The following is the code that is responsible for displaying the post meta.

To display the last modified date, you can add the following code right after the code above.

$u_time = get_the_time('U'); 
$u_modified_time = get_the_modified_time('U'); 
if ($u_modified_time >= $u_time + 86400) { 
echo " | Updated "; 
the_modified_time('F j, Y'); 
}

The code will look like the following.

2. Adding a new function

If you have tried the method above but didn’t work, or simply can’t find the code, you can add a new function to the functions.php file instead. To do so, login to your WordPress dashboard (make sure to login as an administrator) and go to Appearances -> Theme Editor. Select functions.php on the right panel.

Add the following code to the file.

function show_last_modified_date( $content ) {
$original_time = get_the_time('U');
$modified_time = get_the_modified_time('U');
if ($modified_time >= $original_time + 86400) {
$updated_time = get_the_modified_time('h:i a');
$updated_day = get_the_modified_time('F j, Y');
$modified_content .= '<p class="last-modified"><span style="background-color: #FFFF00"><i>Last updated:</i></span> '. $updated_day . '</p>';
}
$modified_content .= $content;
return $modified_content;
}
add_filter( 'the_content', 'show_last_modified_date' );

If you have no idea where to place the code within the file, simply put it right after the last line of the existing code.

The function above will check if the published date of a post and the last modified are different. If yes, it will display the last modified date before the content.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

MOST-READ THIS WEEK