Using WordPress ‘get_post_type_archive_template()’ PHP function

The get_post_type_archive_template() WordPress PHP function retrieves the path of a post type archive template in the current or parent template.

Usage

$template_path = get_post_type_archive_template();

Parameters

This function has no parameters.

More information

See WordPress Developer Resources: get_post_type_archive_template()

Examples

Get and display post type archive template path

This example retrieves the post type archive template path and displays it.

$template_path = get_post_type_archive_template();
echo 'The post type archive template path is: ' . $template_path;

Check if post type archive template exists

This example checks if a post type archive template exists and displays a message accordingly.

$template_path = get_post_type_archive_template();

if ($template_path) {
    echo 'A post type archive template exists.';
} else {
    echo 'No post type archive template found.';
}

Load post type archive template

This example loads the post type archive template if it exists.

$template_path = get_post_type_archive_template();

if ($template_path) {
    load_template($template_path);
} else {
    echo 'No post type archive template found.';
}

Modify post type archive template path

This example modifies the post type archive template path using the ‘archive_template_hierarchy’ filter.

function custom_archive_template_hierarchy($templates) {
    $new_template = 'custom-archive-template.php';
    array_unshift($templates, $new_template);
    return $templates;
}
add_filter('archive_template_hierarchy', 'custom_archive_template_hierarchy');

Display custom content on post type archive template

This example displays custom content on the post type archive template by hooking into the ‘the_content’ filter.

function custom_content_on_archive($content) {
    if (is_post_type_archive()) {
        $content .= '<p>This is custom content added to the post type archive.</p>';
    }
    return $content;
}
add_filter('the_content', 'custom_content_on_archive');