WP_Theme::get_post_templates()

Returns the theme’s post templates.


Description Description


Return Return

(array) Array of page templates, keyed by filename and post type, with the value of the translated header name.


Top ↑

Source Source

File: wp-includes/class-wp-theme.php

	public function get_post_templates() {
		// If you screw up your current theme and we invalidate your parent, most things still work. Let it slide.
		if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) ) {
			return array();
		}

		$post_templates = $this->cache_get( 'post_templates' );

		if ( ! is_array( $post_templates ) ) {
			$post_templates = array();

			$files = (array) $this->get_files( 'php', 1, true );

			foreach ( $files as $file => $full_path ) {
				if ( ! preg_match( '|Template Name:(.*)$|mi', file_get_contents( $full_path ), $header ) ) {
					continue;
				}

				$types = array( 'page' );
				if ( preg_match( '|Template Post Type:(.*)$|mi', file_get_contents( $full_path ), $type ) ) {
					$types = explode( ',', _cleanup_header_comment( $type[1] ) );
				}

				foreach ( $types as $type ) {
					$type = sanitize_key( $type );
					if ( ! isset( $post_templates[ $type ] ) ) {
						$post_templates[ $type ] = array();
					}

					$post_templates[ $type ][ $file ] = _cleanup_header_comment( $header[1] );
				}
			}

			$this->cache_add( 'post_templates', $post_templates );
		}

		if ( $this->load_textdomain() ) {
			foreach ( $post_templates as &$post_type ) {
				foreach ( $post_type as &$post_template ) {
					$post_template = $this->translate_header( 'Template Name', $post_template );
				}
			}
		}

		return $post_templates;
	}

Top ↑

Changelog Changelog

Changelog
Version Description
4.7.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Jonas Lundman

    As of 4.7 this method opens up a great world of the use template files for diffrent purposes. If you save a file within your Theme folder templates/example.php like this:

    <?php 
    /**
     * Template Name: No sidebar and no images
     * Template Post Type: my_post_type
     *

    Then call:

    $templates = wp_get_theme()->get_post_templates();

    You got an array something like this:

    array(4) {
      ["my_post_type"]=>
      array(1) {
        ["templates/example.php"]=>
        string(18) "No sidebar and no images"
      }
      ["locations"]=>
      array(2) {
        ["templates/locations-max.php"]=>
        string(21) "Locations, no sidebar",
        ["templates/locations-no-maps.php"]=>
        string(21) "Locations, information only"
      }
      ["post"]=>
      array(1) {
        ["templates/full-width.php"]=>
        string(21) "Full page, no sidebar"
      }
      ["page"]=>
      array(1) {
        ["templates/full-width.php"]=>
        string(21) "Full page, no sidebar"
      }
    }

    If you only need an array of templates with specific post type, use get_page_templates() instead, as that function has a filter that many Themes and plugin gonna use with the 4.7 features.

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