Would you like to show today’s date on your WordPress website?
Many news sites and blogs update frequently and display the current date to indicate the latest content to their readers. In this guide, we’ll explain how to easily display today’s date or current time on your WordPress site.
Why Display Today’s Date in WordPress?
Showing today’s date is common on news websites, helping visitors know they are viewing the most recent articles. It’s also useful for blogs to indicate the freshness of content to readers.
Other scenarios where showing the current date is helpful include customer support hours in a live chat or creating urgency with a countdown timer.
Let’s explore two straightforward methods to display today’s date on your WordPress site:
Method 1: Displaying Today’s Date by Adding Code to a Template File
WordPress doesn’t have a built-in widget for displaying the current date or time, but you can easily add a snippet of code to your theme files. This code retrieves the current date based on your WordPress settings:
php
echo date(get_option('date_format'));
You can customize the date format by adjusting the PHP code to suit your preferences.
Method 2: Displaying Today’s Date Anywhere Using Shortcode (Recommended)
For flexibility, you can create a shortcode to display the date anywhere on your site without editing theme files directly. Here’s how:
- Install and activate the WPCode plugin on your WordPress site.
- Navigate to
Code Snippets » + Add Snippet
in your WordPress dashboard. - Name your snippet and paste the following PHP code:
php
function wpb_date_today( $atts, $content = null ) {
$atts = shortcode_atts( array(
'format' => '',
), $atts );
$date_time = ”;
if ( $atts[‘format’] == ” ) {
$date_time .= date( get_option( ‘date_format’ ) );
} else {
$date_time .= date( $atts[‘format’] );
}
return $date_time;
}
add_shortcode( ‘date-today’, ‘wpb_date_today’ );
- Customize the shortcode by specifying a date format if needed:
[date-today]
for default date format.[date-today format='F j, Y']
for custom format.
By following these methods, you can easily and effectively display today’s date or time on your WordPress site, enhancing user experience and providing timely information.