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

Returns the theme’s post templates for a given post type.

Parameters

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

Default:null

$post_typestringoptional
Post type to get the templates for. Default 'page'.
If a post is provided, its post type is used.

Default:'page'

Return

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

Source

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;
}

Hooks

apply_filters( ‘theme_templates’, string[] $post_templates, WP_Theme $theme, WP_Post|null $post, string $post_type )

Filters list of page templates for a theme.

apply_filters( “theme_{$post_type}_templates”, string[] $post_templates, WP_Theme $theme, WP_Post|null $post, string $post_type )

Filters list of page templates for a theme.

Changelog

VersionDescription
4.7.0Added the $post_type parameter.
3.4.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    As of 4.7 a safe way to get your custom post type available templates, outside the loop or if the global $post is intercepted:

    $templates = wp_get_theme()->get_page_templates(null, 'my_post_type');

    For a custom post type 'locations' this would give you something “like”:

    array(2) {
      ["templates/locations-max.php"]=>
      string(21) "Locations, no sidebar",
      ["templates/locations-no-maps.php"]=>
      string(21) "Locations, information only"
    }

    As of 4.7 this might be filtered and then some template information might be missing, you can call wp_get_theme()->get_post_templates() instead, then you get an array with all post_types available templates within a deeper array.

  2. Skip to note 4 content

    inside your page template file,example: page-twoColumns.php use this code:

    $myModels = wp_get_theme()->get_page_templates();
    	foreach ( $myModels as $template_name => $template_filename ) {
        echo "$template_name ($template_filename)<br />";
    	}
    	print_r( wp_get_theme()->get_page_templates() );
     

    it will return all available custom page templates for your theme.

    Output of type:

    page-threeColumns.php (threeColumns)
    page-twoColumns.php (twoColumns)
    Array ( [page-threeColumns.php] => threeColumns [page-twoColumns.php] => twoColumns )

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