How to Display Last Updated Date in GeneratePress Theme

GeneratePress is a highly regarded, adaptable WordPress theme known for its performance and flexibility. A feature that many website owners find valuable is the ability to display both the original post date and the last updated date on their blog posts. This enhances transparency and shows that the content is current and accurate. This guide will walk you through the steps of displaying the last updated date in the GeneratePress theme using a PHP snippet.

The Importance of Displaying the Last Updated Date

The last updated date on your blog posts offers several advantages:

  • Increased transparency: By indicating when a post was last updated, you give your readers a clear sense of how recent and relevant the content is.
  • SEO benefits: Search engines like Google favor fresh and updated content. Showing the last updated date can boost your search engine rankings.
  • Boosted credibility: Regularly updating your content and displaying the last updated date assures your readers that you’re actively maintaining your website and providing the most recent information.

Displaying Both Originally Posted and Last Updated Dates

To display both the original post date and the last updated date in GeneratePress using a PHP snippet, follow these steps:

  1. Log in to your WordPress dashboard.
  2. Go to Appearance > Theme Editor.
  3. Locate the functions.php file in your child theme (if you’re not using a child theme, it’s recommended to create one before editing the functions.php file).
  4. Add the following PHP code snippet at the end of the functions.php file:
function custom_generate_post_date_output( $output, $time_string ) {
    $published_time = sprintf(
        'Originally posted on <time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>',
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() )
    );

    if ( get_the_time( 'U' ) === get_the_modified_time( 'U' ) || get_the_date() === get_the_modified_date() ) {
        return sprintf( '<span class="posted-on">%s</span> ', $published_time );
    }

    $updated_time = sprintf(
        'Last Updated on <time class="entry-date updated-date" datetime="%1$s" itemprop="dateModified">%2$s</time>',
        esc_attr( get_the_modified_date( 'c' ) ),
        esc_html( get_the_modified_date() )
    );

    return sprintf( '<span class="posted-on">%s. %s</span> ', $published_time, $updated_time );
}
add_filter( 'generate_post_date_output', 'custom_generate_post_date_output', 10, 2 );
  1. Click “Update File” to save your changes.

This PHP snippet will alter the post date output in GeneratePress to display both the original post date and the last updated date.

Option to Display Either the Original Post Date or the Last Updated Date

In certain scenarios, you might want to display the original post date and only show the last updated date when the post has been updated. This can be achieved by slightly modifying the PHP code snippet. Here’s how you can do it:

function custom_generate_post_date_output( $output, $time_string ) {
    $published_time = sprintf(
        'Originally posted on <time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>',
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() )
    );

    if ( get_the_time( 'U' ) === get_the_modified_time( 'U' ) || get_the_date() === get_the_modified_date() ) {
        return sprintf( '<span class="posted-on">%s</span> ', $published_time );
    }

    $updated_time = sprintf(
        'Last Updated on <time class="entry-date updated-date" datetime="%1$s" itemprop="dateModified">%2$s</time>',
        esc_attr( get_the_modified_date( 'c' ) ),
        esc_html( get_the_modified_date() )
    );

    return sprintf( '<span class="posted-on">%s. %s</span> ', $published_time, $updated_time );
}
add_filter( 'generate_post_date_output', 'custom_generate_post_date_output', 10, 2 );

This modified PHP snippet will display the original post date by default. However, if the post has been updated, it will show both the original post date and the last updated date. This gives your readers a clear idea of the freshness of your content, enhancing their user experience.

Conclusion

Displaying the last updated date in the GeneratePress theme can significantly enhance the transparency of your content, potentially improving your search engine rankings. The PHP snippets provided in this guide will help you effortlessly display both the original post date and the last updated date, or show the last updated date only when a post has been modified. Remember to follow best practices and use a child theme when making changes to your theme files to prevent potential issues with future updates.

Relevant Links and Additional Resources

  1. GeneratePress Documentation – The official documentation for the GeneratePress theme provides comprehensive information on theme features, customization options, and more.
  2. WordPress Codex – A repository of information and resources about WordPress, including tutorials, guides, and reference materials to help you build and customize your WordPress site.
  3. WordPress Hooks – A detailed guide on WordPress hooks, including actions and filters and how to use them for customizing your theme and plugins.
  4. PHP Functions and Filters – The official PHP documentation provides information on PHP functions, filters, and other programming concepts to help you write custom code for your WordPress site.