WP_Theme::get_page_templates() WordPress Method
The WP_Theme::get_page_templates() method can be used to get an array of page templates available for a theme. This is useful if you need to programmatically select a template for a specific page.
WP_Theme::get_page_templates( WP_Post|null $post = null, string $post_type = 'page' ) #
Returns the theme’s post templates for a given post type.
Parameters
- $post
(WP_Post|null)(Optional) The post being edited, provided for context.
Default value: null
- $post_type
(string)(Optional) Post type to get the templates for. If a post is provided, its post type is used.
Default value: 'page'
Return
(string[]) Array of template header names keyed by the template file name.
Source
File: wp-includes/class-wp-theme.php
public function get_page_templates( $post = null, $post_type = 'page' ) { if ( $post ) { $post_type = get_post_type( $post ); } $post_templates = $this->get_post_templates(); $post_templates = isset( $post_templates[ $post_type ] ) ? $post_templates[ $post_type ] : array(); /** * Filters list of page templates for a theme. * * @since 4.9.6 * * @param string[] $post_templates Array of template header names keyed by the template file name. * @param WP_Theme $theme The theme object. * @param WP_Post|null $post The post being edited, provided for context, or null. * @param string $post_type Post type to get the templates for. */ $post_templates = (array) apply_filters( 'theme_templates', $post_templates, $this, $post, $post_type ); /** * Filters list of page templates for a theme. * * The dynamic portion of the hook name, `$post_type`, refers to the post type. * * Possible hook names include: * * - `theme_post_templates` * - `theme_page_templates` * - `theme_attachment_templates` * * @since 3.9.0 * @since 4.4.0 Converted to allow complete control over the `$page_templates` array. * @since 4.7.0 Added the `$post_type` parameter. * * @param string[] $post_templates Array of template header names keyed by the template file name. * @param WP_Theme $theme The theme object. * @param WP_Post|null $post The post being edited, provided for context, or null. * @param string $post_type Post type to get the templates for. */ $post_templates = (array) apply_filters( "theme_{$post_type}_templates", $post_templates, $this, $post, $post_type ); return $post_templates; }
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
4.7.0 | Added the $post_type parameter. |
3.4.0 | Introduced. |