How to Display Last Modified Date Beneath Content in WordPress Posts

Adding the last modified date to your WordPress posts can offer visitors valuable insights into the currency of your content. This tutorial walks you through the process of integrating this feature using PHP code. By following these steps, you’ll be able to seamlessly present the last modified date in posts without resorting to plugins.

Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.

Access Your Theme’s Functions.php File

Open your theme’s functions.php file using a text editor or the WordPress theme editor and add the following code snippet to the functions.php file:

function display_last_modified_date() {
    $u_time = get_the_time( 'U' );
    $u_modified_time = get_the_modified_time( 'U' );

    // Show modified date if 24 hours have passed since the post was published.
    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' );

        echo '<p class="last-updated">';
            // Translators: Insert the date and time of modification.
            esc_html__( 'Last updated on %1$s at %2$s', 'your-text-domain' ),
        echo '</p>';

function append_last_modified_date( $content ) {
    if ( is_single() ) {
        $last_modified = ob_get_clean();

        return $content . $last_modified;
    return $content;
add_filter( 'the_content', 'append_last_modified_date' );
Code language: PHP (php)

Code Explanation:

  • The display_last_updated_date() function calculates the original publishing time and the last modified time of the post using the get_the_time() and get_the_modified_time() functions, respectively.
  • It then checks if at least 24 hours have passed since the post was published.
  • If the condition is met, it constructs the HTML markup to display the last updated information.
  • append_last_modified_date() hooks into the the_content filter and adds the last modified date output to the content of single post pages.

Save and Preview

After adding the code snippet, click the “Update File” button to save the changes to your theme’s functions.php file. Now, preview any of your posts that have been modified within the last 24 hours. You should see the last updated information displayed below the post content.

That’s it! By following this tutorial, you’ve successfully incorporated the display of last modified dates beneath your WordPress posts. This customization ensures that your readers stay informed about content updates, without the need for external plugins.

Leave your feedback and help us improve 🐶

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee ☕️ or donating via PayPal 💰.

More free knowledge, because why not?

Your thoughts matter, leave a reply 💬

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