get_page_templates()WP 1.5.0

Get the Page Templates available in this theme

No Hooks.


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


get_page_templates( $post, $post_type );
The post being edited, provided for context.
Default: null
Post type to get the templates for.
Default: 'page'



#1 Using the get_page_templates() method of the WP_Theme class

It's more convenient and correct to use get_page_templates() method instead of this function. In this case you don't need to include the necessary file:

$templates = wp_get_theme()->get_page_templates();
print_r( $templates );


	[my-custom-page.php] => My permanent page template
	[my-page-templates/my-second-page.php] => Another template for a permanent page

As you see here, the key is the name of the file, and the value is the name of the template.


#2 Let's display the names of static page templates

Suppose we have 2 templates for static pages in our theme: "Archive Page" and "One Column, No Navigation Menu":

include( ABSPATH . '/wp-admin/includes/theme.php' );

$templates = get_page_templates();

print_r( $templates );

/* will return:
Array (
	[Archive page] => page_archive-template.php
	[One-column, no navigation menu] => page_one-column-template.php

Now Display it:

foreach ( $templates as $template_name => $template_filename ) {
   echo "$template_name ($template_filename)<br />";

/* Returns:
Archive page (page_archive-template.php)
One column, no navigation menu (page_one-column-template.php)


Since 1.5.0 Introduced.
Since 4.7.0 Added the $post_type parameter.

get_page_templates() code WP 6.5.3

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