How to Automatically Display the Last Updated Date in Your WordPress Site's Header
As a WordPress website owner, it's important to keep your content up-to-date and relevant for your users. One way to showcase this is by displaying the last updated date on your site. This not only helps users understand how fresh your content is, but it can also improve your SEO by demonstrating to search engines that your site is actively maintained.
In this blog post, we'll walk you through the steps to automatically display the last updated date in your WordPress site's header using a custom function and shortcode.
Why Display the Last Updated Date?
Showing the last updated date on your WordPress site offers several benefits:
-
Improved User Experience: When users visit your site, they can quickly see how recent the content is, which helps them gauge the relevance and trustworthiness of the information.
-
Enhanced SEO: Search engines like Google tend to favor websites that regularly update their content. Displaying the last updated date is a signal that your site is actively maintained, which can improve your search engine rankings.
-
Increased Transparency: Providing the last updated date demonstrates transparency and builds trust with your audience, as they can see that you are committed to keeping your content current.
Adding the Last Updated Date to Your WordPress Header
To display the last updated date in your WordPress site's header, we'll need to create a custom function and a shortcode. Here's how you can do it:
-
Create the Custom Function:
Open your WordPress site's functions.php
file (usually located in the theme directory) and add the following code:
<?php
function last_updated(){
global $wpdb;
$sql = "SELECT post_modified
FROM $wpdb->posts
WHERE post_type='post' OR post_type='product'
AND post_status='publish'
ORDER BY post_modified DESC
LIMIT 1";
$last_update = $wpdb->get_var( $sql );
$last_update = date("d-m-Y", strtotime($last_update));
return $last_update;
}
add_shortcode('last_updated_date','last_updated');
?>
This function retrieves the latest modified date for all published posts and products on your WordPress site, formats the date, and returns it.
-
Display the Last Updated Date in the Header:
Next, open your site's header.php
file and add the following code where you want the last updated date to appear:
<?php echo do_shortcode('[last_updated_date]'); ?>
This will display the last updated date using the shortcode we created in the custom function.
With these changes, your WordPress site will now automatically display the last updated date in the header, providing your users with up-to-date information about the freshness of your content.
Understanding the Custom Function
Let's break down the custom function we created in the functions.php
file:
function last_updated(){
global $wpdb;
$sql = "SELECT post_modified
FROM $wpdb->posts
WHERE post_type='post' OR post_type='product'
AND post_status='publish'
ORDER BY post_modified DESC
LIMIT 1";
$last_update = $wpdb->get_var( $sql );
$last_update = date("d-m-Y", strtotime($last_update));
return $last_update;
}
-
global $wpdb;
: This line allows us to access the WordPress database object, which we'll use to retrieve the latest post modification date.
-
$sql = "SELECT post_modified ...";
: This SQL query selects the post_modified
date for the latest published post or product on your site, ordered by the most recent modification date.
-
$last_update = $wpdb->get_var( $sql );
: This line executes the SQL query and stores the result (the latest modification date) in the $last_update
variable.
-
$last_update = date("d-m-Y", strtotime($last_update));
: This converts the MySQL date format to a more user-friendly format (day-month-year).
-
return $last_update;
: Finally, the function returns the formatted last updated date.
After creating this custom function, we add a shortcode to it using add_shortcode('last_updated_date','last_updated')
. This allows us to easily display the last updated date anywhere on our WordPress site by using the [last_updated_date]
shortcode.
Displaying the Last Updated Date in Your Header
In the header.php
file, we use the do_shortcode()
function to render the [last_updated_date]
shortcode and output the last updated date:
<?php echo do_shortcode('[last_updated_date]'); ?>
This code will insert the last updated date wherever you place it in your site's header.
Real-World Example and Benefits
Let's consider a real-world example of how displaying the last updated date can benefit your WordPress site:
Imagine you're running a blog about the latest tech trends. Your readers expect you to provide them with up-to-date information, as the tech industry is constantly evolving. By displaying the last updated date in your site's header, you're giving your users a clear indication of how recent your content is.
This can have several positive impacts:
-
Improved User Experience: Your readers will immediately know that your content is fresh and relevant, which can increase their trust and engagement with your site.
-
Enhanced SEO: Search engines like Google favor websites that regularly update their content. Displaying the last updated date is a signal that your site is actively maintained, which can improve your search engine rankings.
-
Increased Credibility: Showing the last updated date demonstrates that you're committed to providing your audience with the most current information, which can enhance your credibility as a thought leader in your industry.
By implementing this simple feature, you're taking a step towards providing a better user experience and improving the overall performance of your WordPress site.
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.
Conclusion
Displaying the last updated date in your WordPress site's header is a straightforward and effective way to keep your users informed about the freshness of your content. By creating a custom function and shortcode, you can easily integrate this feature into your site and reap the benefits of improved user experience, enhanced SEO, and increased credibility.
Remember, keeping your content up-to-date is crucial for the success of your WordPress site. By implementing this last updated date feature, you're showing your audience that you're committed to providing them with the most relevant and timely information. Flowpoint.ai can help you identify other technical improvements to boost your website's conversion rates and user engagement