Random WordPress Function

Learn about a new WordPress function every day!


Function Signature:

get_post_type_archive_template

Function Description:

Retrieves path of post type archive template in current or parent template.

Function Examples:

⚠️ Examples below are generated with GPT-3 once every hour. Do not take them too seriously.
Consider them as some extra input in your learning process - reason about them. Will it work? What could fail?
// Example 1: Getting the post type archive template for the default post type
$post_type = 'post';
$template = get_post_type_archive_template( $post_type );
echo "The template for the post type archive of $post_type is: $template";
// Example 2: Customizing the post type archive template for a custom post type
$post_type = 'books';
$template = get_post_type_archive_template( $post_type );
if ( ! $template ) {
    $template = 'custom-books-archive.php'; // Custom template file for books post type archive
}
echo "The template for the post type archive of $post_type is: $template";
// Example 3: Using get_post_type_archive_template within a custom function
function custom_post_type_archive_template( $post_type ) {
    $template = get_post_type_archive_template( $post_type );
    if ( ! $template ) {
        $template = 'custom-archive.php'; // Custom template file for the specified post type archive
    }
    return $template;
}

$post_type = 'movies';
$template = custom_post_type_archive_template( $post_type );
echo "The template for the post type archive of $post_type is: $template";