How to display updated date for WordPress posts with GeneratePress

On one of the WordPress blogs we run we wanted to display the updated date as opposed to the published date on posts and on archive pages. After a quick Google search, we found a snippet of code we could use in a GeneratePress Premium Theme Element.

GeneratePress Elements means no more functions.php

Okay, that might be an overstatement. Well, maybe it isn’t. Using GeneratePress Elements you can do a number of really cool things including making custom headers, footers, blocks and adding HTML, shortcakes and PHP to your GeneratePress site.

First, let’s take a look at the code to display the date a post was last updated as opposed to when it was published.

PHP Code to Display the Updated Date instead of the Published Date

Credit where credit is due: We did not write the code below. A link to this code was suggested in the GeneratePress user forums. The website it is from is called WPLOGOUT and the author of the post is Suraj Katwal. The only different in our use is that we’re using a GeneratePress Element whereas they are using the Code Snippets plugin. You can check out their post here.

<?php
add_filter( 'generate_post_date_output', function( $output, $time_string ) {
    $time_string = '%2$s';

    if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
        $time_string = 'Last Updated on: %4$s';
    }

    $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( '%s ',
        $time_string
    );
}, 10, 2 );
?>

If we were using another theme, we’d either open up the functions.php file to add this code. Or, we might use the Snippets plugin. But, with GeneratePress Premium Elements, we can add this code right in the WordPress Admin.

How to display the updated date in GeneratePress Premium

As a pre-requisite, which seems obvious, the GeneratePress Theme (or a GeneratePress child theme) needs to be enabled and GeneratePress Premium is required.

Time needed: 3 minutes.

Follow these simple steps and on any page on your site where a post date is shown, the updated date will be displayed as opposed to the date the post was published.

  1. As necessary, enable the GeneratePress Elements Module

    In the WordPress Admin navigate to Appearance > GeneratePress and ensure the Elements Module is activated.Enable the GeneratePress Elements Module

  2. Open the Elements Module and create a new element.

    Navigate to Appearance > Elements. Any existing Elements you have previously created will be shown on the page. Click the Add New Element button and when prompted select the Hook Element type and click the Create button.Create a New Hook Element

  3. Name your Element and enter the post edited date replacement code from above.

    Pick a name that will make sense to you later, then cut-and-paste the code from above into the main content area. Don’t forget to include both the opening and closing PHP tags.
    Element Hook and Code

  4. Complete the Settings for the Hook Element

    Below where the code was just pasted, set the Hook to wp_head and enabled the Execute PHP option.Select Hook and Enable Execute PHP

  5. Set where the Element should be displayed.

    Click on the Display Rules tab and indicate which page or pages this Element to execute. We wanted the updated date to be shown everywhere. We opted to run the PHP code site-wide for the time being. The three simple options allow for reasonably granular display rules.Set Display Rules for your WordPress GeneratePress Site

  6. Save the Element

    Once your code and settings are complete, click the Publish button to enable the Element. On your site you will now see the post updated date in place of the published date.

GeneratePress Elements

If this post served as an introduction to GeneratePress Premium Elements, we hope you can see how they might be worthwhile to consider for your WordPress projects. Check out our GeneratePress Review, and let us know if you have any questions about the free or Premium GeneratePress theme. We use it on all of our sites, both client sites and our own.

Leave a Comment

Share via
Copy link
Powered by Social Snap