get_page_templates( WP_Post|null $post = null, string $post_type = ‘page’ ): string[]

Gets the page templates available in this theme.

Parameters

$postWP_Post|nulloptional
The post being edited, provided for context.

Default:null

$post_typestringoptional
Post type to get the templates for. Default 'page'.

Default:'page'

Return

string[] Array of template file names keyed by the template header name.

More Information

The function searches all the current theme’s template files for the commented “Template Name: name of template”. See also wp_get_theme() and the wp_get_theme() ->get_page_templates() method of the WP_Theme class.

Source

function get_page_templates( $post = null, $post_type = 'page' ) {
	return array_flip( wp_get_theme()->get_page_templates( $post, $post_type ) );
}

Changelog

VersionDescription
4.7.0Added the $post_type parameter.
1.5.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Output a list of available templates

    The following code snippet loops through the available page templates and outputs their template names and the filenames.

    <?php 
    $templates = wp_get_theme()->get_page_templates();
    print_r( $templates );
    ?>

    Returns:

    Array (
    	[template-contact.php] => Contact
    	[template-landing-page.php] => Landing Page
    	[template-another-template.php] => Another Template
    )

You must log in before being able to contribute a note or feedback.