wp/wp-includes/template.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
    16  *
    16  *
    17  * @since 1.5.0
    17  * @since 1.5.0
    18  *
    18  *
    19  * @param string $type Filename without extension.
    19  * @param string $type Filename without extension.
    20  * @param array $templates An optional list of template candidates
    20  * @param array $templates An optional list of template candidates
    21  * @return string Full path to file.
    21  * @return string Full path to template file.
    22  */
    22  */
    23 function get_query_template( $type, $templates = array() ) {
    23 function get_query_template( $type, $templates = array() ) {
    24 	$type = preg_replace( '|[^a-z0-9-]+|', '', $type );
    24 	$type = preg_replace( '|[^a-z0-9-]+|', '', $type );
    25 
    25 
    26 	if ( empty( $templates ) )
    26 	if ( empty( $templates ) )
    28 
    28 
    29 	$template = locate_template( $templates );
    29 	$template = locate_template( $templates );
    30 	/**
    30 	/**
    31 	 * Filter the path of the queried template by type.
    31 	 * Filter the path of the queried template by type.
    32 	 *
    32 	 *
    33 	 * The dynamic portion of the hook name, $type, refers to the filename
    33 	 * The dynamic portion of the hook name, `$type`, refers to the filename
    34 	 * -- minus the extension -- of the file to load. This hook also applies
    34 	 * -- minus the extension -- of the file to load. This hook also applies
    35 	 * to various types of files loaded as part of the Template Hierarchy.
    35 	 * to various types of files loaded as part of the Template Hierarchy.
    36 	 *
    36 	 *
    37 	 * @since 1.5.2
    37 	 * @since 1.5.0
    38 	 *
    38 	 *
    39 	 * @param string $template Path to the template. @see locate_template()
    39 	 * @param string $template Path to the template. See {@see locate_template()}.
    40 	 */
    40 	 */
    41 	return apply_filters( "{$type}_template", $template );
    41 	return apply_filters( "{$type}_template", $template );
    42 }
    42 }
    43 
    43 
    44 /**
    44 /**
    45  * Retrieve path of index template in current or parent template.
    45  * Retrieve path of index template in current or parent template.
    46  *
    46  *
       
    47  * The template path is filterable via the 'index_template' hook.
       
    48  *
    47  * @since 3.0.0
    49  * @since 3.0.0
    48  *
    50  *
    49  * @return string
    51  * @see get_query_template()
       
    52  *
       
    53  * @return string Full path to index template file.
    50  */
    54  */
    51 function get_index_template() {
    55 function get_index_template() {
    52 	return get_query_template('index');
    56 	return get_query_template('index');
    53 }
    57 }
    54 
    58 
    55 /**
    59 /**
    56  * Retrieve path of 404 template in current or parent template.
    60  * Retrieve path of 404 template in current or parent template.
    57  *
    61  *
    58  * @since 1.5.0
    62  * The template path is filterable via the '404_template' hook.
    59  *
    63  *
    60  * @return string
    64  * @since 1.5.0
       
    65  *
       
    66  * @see get_query_template()
       
    67  *
       
    68  * @return string Full path to 404 template file.
    61  */
    69  */
    62 function get_404_template() {
    70 function get_404_template() {
    63 	return get_query_template('404');
    71 	return get_query_template('404');
    64 }
    72 }
    65 
    73 
    66 /**
    74 /**
    67  * Retrieve path of archive template in current or parent template.
    75  * Retrieve path of archive template in current or parent template.
    68  *
    76  *
    69  * @since 1.5.0
    77  * The template path is filterable via the 'archive_template' hook.
    70  *
    78  *
    71  * @return string
    79  * @since 1.5.0
       
    80  *
       
    81  * @see get_query_template()
       
    82  *
       
    83  * @return string Full path to archive template file.
    72  */
    84  */
    73 function get_archive_template() {
    85 function get_archive_template() {
    74 	$post_types = array_filter( (array) get_query_var( 'post_type' ) );
    86 	$post_types = array_filter( (array) get_query_var( 'post_type' ) );
    75 
    87 
    76 	$templates = array();
    88 	$templates = array();
    85 }
    97 }
    86 
    98 
    87 /**
    99 /**
    88  * Retrieve path of post type archive template in current or parent template.
   100  * Retrieve path of post type archive template in current or parent template.
    89  *
   101  *
       
   102  * The template path is filterable via the 'archive_template' hook.
       
   103  *
    90  * @since 3.7.0
   104  * @since 3.7.0
    91  *
   105  *
    92  * @return string
   106  * @see get_archive_template()
       
   107  *
       
   108  * @return string Full path to archive template file.
    93  */
   109  */
    94 function get_post_type_archive_template() {
   110 function get_post_type_archive_template() {
    95 	$post_type = get_query_var( 'post_type' );
   111 	$post_type = get_query_var( 'post_type' );
    96 	if ( is_array( $post_type ) )
   112 	if ( is_array( $post_type ) )
    97 		$post_type = reset( $post_type );
   113 		$post_type = reset( $post_type );
   104 }
   120 }
   105 
   121 
   106 /**
   122 /**
   107  * Retrieve path of author template in current or parent template.
   123  * Retrieve path of author template in current or parent template.
   108  *
   124  *
   109  * @since 1.5.0
   125  * The template path is filterable via the 'author_template' hook.
   110  *
   126  *
   111  * @return string
   127  * @since 1.5.0
       
   128  *
       
   129  * @see get_query_template()
       
   130  *
       
   131  * @return string Full path to author template file.
   112  */
   132  */
   113 function get_author_template() {
   133 function get_author_template() {
   114 	$author = get_queried_object();
   134 	$author = get_queried_object();
   115 
   135 
   116 	$templates = array();
   136 	$templates = array();
   117 
   137 
   118 	if ( is_a( $author, 'WP_User' ) ) {
   138 	if ( $author instanceof WP_User ) {
   119 		$templates[] = "author-{$author->user_nicename}.php";
   139 		$templates[] = "author-{$author->user_nicename}.php";
   120 		$templates[] = "author-{$author->ID}.php";
   140 		$templates[] = "author-{$author->ID}.php";
   121 	}
   141 	}
   122 	$templates[] = 'author.php';
   142 	$templates[] = 'author.php';
   123 
   143 
   129  *
   149  *
   130  * Works by first retrieving the current slug, for example 'category-default.php', and then
   150  * Works by first retrieving the current slug, for example 'category-default.php', and then
   131  * trying category ID, for example 'category-1.php', and will finally fall back to category.php
   151  * trying category ID, for example 'category-1.php', and will finally fall back to category.php
   132  * template, if those files don't exist.
   152  * template, if those files don't exist.
   133  *
   153  *
   134  * @since 1.5.0
   154  * The template path is filterable via the 'category_template' hook.
   135  * @uses apply_filters() Calls 'category_template' on file path of category template.
   155  *
   136  *
   156  * @since 1.5.0
   137  * @return string
   157  *
       
   158  * @see get_query_template()
       
   159  *
       
   160  * @return string Full path to category template file.
   138  */
   161  */
   139 function get_category_template() {
   162 function get_category_template() {
   140 	$category = get_queried_object();
   163 	$category = get_queried_object();
   141 
   164 
   142 	$templates = array();
   165 	$templates = array();
   155  *
   178  *
   156  * Works by first retrieving the current tag name, for example 'tag-wordpress.php', and then
   179  * Works by first retrieving the current tag name, for example 'tag-wordpress.php', and then
   157  * trying tag ID, for example 'tag-1.php', and will finally fall back to tag.php
   180  * trying tag ID, for example 'tag-1.php', and will finally fall back to tag.php
   158  * template, if those files don't exist.
   181  * template, if those files don't exist.
   159  *
   182  *
       
   183  * The template path is filterable via the 'tag_template' hook.
       
   184  *
   160  * @since 2.3.0
   185  * @since 2.3.0
   161  * @uses apply_filters() Calls 'tag_template' on file path of tag template.
   186  *
   162  *
   187  * @see get_query_template()
   163  * @return string
   188  *
       
   189  * @return string Full path to tag template file.
   164  */
   190  */
   165 function get_tag_template() {
   191 function get_tag_template() {
   166 	$tag = get_queried_object();
   192 	$tag = get_queried_object();
   167 
   193 
   168 	$templates = array();
   194 	$templates = array();
   186  * The taxonomy and term template is checked and used first, if it exists.
   212  * The taxonomy and term template is checked and used first, if it exists.
   187  * Second, just the taxonomy template is checked, and then finally, taxonomy.php
   213  * Second, just the taxonomy template is checked, and then finally, taxonomy.php
   188  * template is used. If none of the files exist, then it will fall back on to
   214  * template is used. If none of the files exist, then it will fall back on to
   189  * index.php.
   215  * index.php.
   190  *
   216  *
       
   217  * The template path is filterable via the 'taxonomy_template' hook.
       
   218  *
   191  * @since 2.5.0
   219  * @since 2.5.0
   192  * @uses apply_filters() Calls 'taxonomy_template' filter on found path.
   220  *
   193  *
   221  * @see get_query_template()
   194  * @return string
   222  *
       
   223  * @return string Full path to taxonomy template file.
   195  */
   224  */
   196 function get_taxonomy_template() {
   225 function get_taxonomy_template() {
   197 	$term = get_queried_object();
   226 	$term = get_queried_object();
   198 
   227 
   199 	$templates = array();
   228 	$templates = array();
   209 }
   238 }
   210 
   239 
   211 /**
   240 /**
   212  * Retrieve path of date template in current or parent template.
   241  * Retrieve path of date template in current or parent template.
   213  *
   242  *
   214  * @since 1.5.0
   243  * The template path is filterable via the 'date_template' hook.
   215  *
   244  *
   216  * @return string
   245  * @since 1.5.0
       
   246  *
       
   247  * @see get_query_template()
       
   248  *
       
   249  * @return string Full path to date template file.
   217  */
   250  */
   218 function get_date_template() {
   251 function get_date_template() {
   219 	return get_query_template('date');
   252 	return get_query_template('date');
   220 }
   253 }
   221 
   254 
   222 /**
   255 /**
   223  * Retrieve path of home template in current or parent template.
   256  * Retrieve path of home template in current or parent template.
   224  *
   257  *
   225  * This is the template used for the page containing the blog posts.
   258  * This is the template used for the page containing the blog posts.
   226  *
       
   227  * Attempts to locate 'home.php' first before falling back to 'index.php'.
   259  * Attempts to locate 'home.php' first before falling back to 'index.php'.
   228  *
   260  *
   229  * @since 1.5.0
   261  * The template path is filterable via the 'home_template' hook.
   230  * @uses apply_filters() Calls 'home_template' on file path of home template.
   262  *
   231  *
   263  * @since 1.5.0
   232  * @return string
   264  *
       
   265  * @see get_query_template()
       
   266  *
       
   267  * @return string Full path to home template file.
   233  */
   268  */
   234 function get_home_template() {
   269 function get_home_template() {
   235 	$templates = array( 'home.php', 'index.php' );
   270 	$templates = array( 'home.php', 'index.php' );
   236 
   271 
   237 	return get_query_template( 'home', $templates );
   272 	return get_query_template( 'home', $templates );
   238 }
   273 }
   239 
   274 
   240 /**
   275 /**
   241  * Retrieve path of front-page template in current or parent template.
   276  * Retrieve path of front-page template in current or parent template.
   242  *
   277  *
   243  * Looks for 'front-page.php'.
   278  * Looks for 'front-page.php'. The template path is filterable via the
       
   279  * 'front_page_template' hook.
   244  *
   280  *
   245  * @since 3.0.0
   281  * @since 3.0.0
   246  * @uses apply_filters() Calls 'front_page_template' on file path of template.
   282  *
   247  *
   283  * @see get_query_template()
   248  * @return string
   284  *
       
   285  * @return string Full path to front page template file.
   249  */
   286  */
   250 function get_front_page_template() {
   287 function get_front_page_template() {
   251 	$templates = array('front-page.php');
   288 	$templates = array('front-page.php');
   252 
   289 
   253 	return get_query_template( 'front_page', $templates );
   290 	return get_query_template( 'front_page', $templates );
   258  *
   295  *
   259  * Will first look for the specifically assigned page template.
   296  * Will first look for the specifically assigned page template.
   260  * Then will search for 'page-{slug}.php', followed by 'page-{id}.php',
   297  * Then will search for 'page-{slug}.php', followed by 'page-{id}.php',
   261  * and finally 'page.php'.
   298  * and finally 'page.php'.
   262  *
   299  *
   263  * @since 1.5.0
   300  * The template path is filterable via the 'page_template' hook.
   264  *
   301  *
   265  * @return string
   302  * @since 1.5.0
       
   303  *
       
   304  * @see get_query_template()
       
   305  *
       
   306  * @return string Full path to page template file.
   266  */
   307  */
   267 function get_page_template() {
   308 function get_page_template() {
   268 	$id = get_queried_object_id();
   309 	$id = get_queried_object_id();
   269 	$template = get_page_template_slug();
   310 	$template = get_page_template_slug();
   270 	$pagename = get_query_var('pagename');
   311 	$pagename = get_query_var('pagename');
   289 }
   330 }
   290 
   331 
   291 /**
   332 /**
   292  * Retrieve path of paged template in current or parent template.
   333  * Retrieve path of paged template in current or parent template.
   293  *
   334  *
   294  * @since 1.5.0
   335  * The template path is filterable via the 'paged_template' hook.
   295  *
   336  *
   296  * @return string
   337  * @since 1.5.0
       
   338  *
       
   339  * @see get_query_template()
       
   340  *
       
   341  * @return string Full path to paged template file.
   297  */
   342  */
   298 function get_paged_template() {
   343 function get_paged_template() {
   299 	return get_query_template('paged');
   344 	return get_query_template('paged');
   300 }
   345 }
   301 
   346 
   302 /**
   347 /**
   303  * Retrieve path of search template in current or parent template.
   348  * Retrieve path of search template in current or parent template.
   304  *
   349  *
   305  * @since 1.5.0
   350  * The template path is filterable via the 'search_template' hook.
   306  *
   351  *
   307  * @return string
   352  * @since 1.5.0
       
   353  *
       
   354  * @see get_query_template()
       
   355  *
       
   356  * @return string Full path to search template file.
   308  */
   357  */
   309 function get_search_template() {
   358 function get_search_template() {
   310 	return get_query_template('search');
   359 	return get_query_template('search');
   311 }
   360 }
   312 
   361 
   313 /**
   362 /**
   314  * Retrieve path of single template in current or parent template.
   363  * Retrieve path of single template in current or parent template.
   315  *
   364  *
   316  * @since 1.5.0
   365  * The template path is filterable via the 'single_template' hook.
   317  *
   366  *
   318  * @return string
   367  * @since 1.5.0
       
   368  *
       
   369  * @see get_query_template()
       
   370  *
       
   371  * @return string Full path to single template file.
   319  */
   372  */
   320 function get_single_template() {
   373 function get_single_template() {
   321 	$object = get_queried_object();
   374 	$object = get_queried_object();
   322 
   375 
   323 	$templates = array();
   376 	$templates = array();
   338  * 'attachment.php' is checked and returned.
   391  * 'attachment.php' is checked and returned.
   339  *
   392  *
   340  * Some examples for the 'text/plain' mime type are 'text.php', 'plain.php', and
   393  * Some examples for the 'text/plain' mime type are 'text.php', 'plain.php', and
   341  * finally 'text_plain.php'.
   394  * finally 'text_plain.php'.
   342  *
   395  *
       
   396  * The template path is filterable via the 'attachment_template' hook.
       
   397  *
   343  * @since 2.0.0
   398  * @since 2.0.0
   344  *
   399  *
   345  * @return string
   400  * @see get_query_template()
       
   401  *
       
   402  * @return string Full path to attachment template file.
   346  */
   403  */
   347 function get_attachment_template() {
   404 function get_attachment_template() {
   348 	global $posts;
   405 	global $posts;
   349 
   406 
   350 	if ( ! empty( $posts ) && isset( $posts[0]->post_mime_type ) ) {
   407 	if ( ! empty( $posts ) && isset( $posts[0]->post_mime_type ) ) {
   369  * Retrieve path of comment popup template in current or parent template.
   426  * Retrieve path of comment popup template in current or parent template.
   370  *
   427  *
   371  * Checks for comment popup template in current template, if it exists or in the
   428  * Checks for comment popup template in current template, if it exists or in the
   372  * parent template.
   429  * parent template.
   373  *
   430  *
   374  * @since 1.5.0
   431  * The template path is filterable via the 'comments_popup_template' hook.
   375  * @uses apply_filters() Calls 'comments_popup_template' filter on path.
   432  *
   376  *
   433  * @since 1.5.0
   377  * @return string
   434  *
       
   435  * @see get_query_template()
       
   436  *
       
   437  * @return string Full path to comments popup template file.
   378  */
   438  */
   379 function get_comments_popup_template() {
   439 function get_comments_popup_template() {
   380 	$template = get_query_template( 'comments_popup', array( 'comments-popup.php' ) );
   440 	$template = get_query_template( 'comments_popup', array( 'comments-popup.php' ) );
   381 
   441 
   382 	// Backward compat code will be removed in a future release
   442 	// Backward compat code will be removed in a future release
   405 		if ( !$template_name )
   465 		if ( !$template_name )
   406 			continue;
   466 			continue;
   407 		if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
   467 		if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
   408 			$located = STYLESHEETPATH . '/' . $template_name;
   468 			$located = STYLESHEETPATH . '/' . $template_name;
   409 			break;
   469 			break;
   410 		} else if ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
   470 		} elseif ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
   411 			$located = TEMPLATEPATH . '/' . $template_name;
   471 			$located = TEMPLATEPATH . '/' . $template_name;
   412 			break;
   472 			break;
   413 		}
   473 		}
   414 	}
   474 	}
   415 
   475