get_page_templates()
Get the Page Templates available in this theme
Used By: page_template_dropdown()
No Hooks.
Return
String[]
. Array of template file names keyed by the template header name.
Usage
get_page_templates( $post, $post_type );
- $post(WP_Post|null)
- The post being edited, provided for context.
Default: null - $post_type(string)
- Post type to get the templates for.
Default: 'page'
Examples
#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 );
Returns:
Array ( [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) */
Changelog
Since 1.5.0 | Introduced. |
Since 4.7.0 | Added the $post_type parameter. |
get_page_templates() get page templates code WP 6.7.1
function get_page_templates( $post = null, $post_type = 'page' ) { return array_flip( wp_get_theme()->get_page_templates( $post, $post_type ) ); }