Using WordPress ‘get_archive_template()’ PHP function

The get_archive_template() WordPress PHP function retrieves the path of the archive template in the current or parent theme.

Usage

$path = get_archive_template();

This will store the path of the archive template in the $path variable.

Parameters

  • None

More information

See WordPress Developer Resources: get_archive_template()

Examples

Load archive template

Load the archive template in a custom template file:

$path = get_archive_template();
load_template($path);

Conditionally load a custom archive template

Use a custom archive template for a specific post type:

if (is_post_type_archive('product')) {
    $custom_archive = locate_template('archive-product.php');
    load_template($custom_archive);
} else {
    $path = get_archive_template();
    load_template($path);
}

Modify the archive template

Filter the archive template path to use a custom template:

function my_custom_archive_template($archive_template) {
    return locate_template('my-custom-archive.php');
}
add_filter('archive_template', 'my_custom_archive_template');

Check if archive template exists

Verify if the archive template exists in the current theme:

$path = get_archive_template();
if ($path) {
    echo "Archive template exists!";
} else {
    echo "No archive template found!";
}

Display archive template path

Output the path of the archive template:

$path = get_archive_template();
echo "The archive template path is: " . $path;