Using WordPress ‘get_date_template()’ PHP function

The get_date_template() WordPress PHP function retrieves the path of the date template in the current or parent template.

Usage

To use the get_date_template() function, simply call the function like this:

$template_path = get_date_template();

Parameters

This function has no parameters.

More information

See WordPress Developer Resources: get_date_template()

Examples

Load date template

Load the date template in your theme:

$template_path = get_date_template();
load_template($template_path);

Check if date template exists

Check if a date template exists before loading it:

$template_path = get_date_template();
if (!empty($template_path)) {
    load_template($template_path);
}

Modify the output of the date template

Filter the output of the date template:

add_filter('date_template', function($template) {
    // Modify the $template variable as needed.
    return $template;
});

Custom template for specific year

Use a custom template for a specific year:

add_filter('date_template', function($template) {
    if (is_year('2023')) {
        $new_template = locate_template(array('year-2023.php'));
        if (!empty($new_template)) {
            return $new_template;
        }
    }
    return $template;
});

Display custom message when no date template is found

Display a custom message when no date template is found:

$template_path = get_date_template();
if (!empty($template_path)) {
    load_template($template_path);
} else {
    echo '<p>Sorry, no date template was found.</p>';
}