apply_filters( "{$type}_template", string $template, string $type, array $templates )

Filters the path of the queried template by type.


Description Description

The dynamic portion of the hook name, $type, refers to the filename — minus the file extension and any non-alphanumeric characters delimiting words — of the file to load. This hook also applies to various types of files loaded as part of the Template Hierarchy.

Possible values for $type include: ‘index’, ‘404’, ‘archive’, ‘author’, ‘category’, ‘tag’, ‘taxonomy’, ‘date’, ’embed’, ‘home’, ‘frontpage’, ‘privacypolicy’, ‘page’, ‘paged’, ‘search’, ‘single’, ‘singular’, and ‘attachment’.


Top ↑

Parameters Parameters

$template

(string) Path to the template. See locate_template().

$type

(string) Sanitized filename without extension.

$templates

(array) A list of template candidates, in descending order of priority.


Top ↑

Source Source

File: wp-includes/template.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
4.8.0 The $type and $templates parameters were added.
1.5.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Al

    We can use this filter in case that we want to add and archive page for our custom taxonomy that we have created from a plugin. In this case the filter we will need is “taxonomy_template”.

    add_filter( "taxonomy_template", 'load_our_custom_tax_template');
    function load_our_custom_tax_template ($tax_template) {
      if (is_tax('custom-tax-name')) {
        $tax_template = dirname(  __FILE__  ) . '/templates/custom-taxonomy-template.php';
      }
      return $tax_template;
    }
    
  2. Skip to note 2 content
    Contributed by stevenlinx

    Migrated from Codex:

    The example code will load the template file “post-type-template.php” located in your plugins folder for any posts or pages that have the type of ‘my_post_type‘ else uses default template.

    <?php
    function get_custom_post_type_template( $single_template ) {
    	global $post;
    
    	if ( 'my_post_type' === $post->post_type ) {
    		$single_template = dirname( __FILE__ ) . '/post-type-template.php';
    	}
    
    	return $single_template;
    }
    add_filter( 'single_template', 'get_custom_post_type_template' );
    ?>
    
  3. Skip to note 3 content
    Contributed by stevenlinx

    This example loads the template file single-{post_type}-{slug}.php (e.g. single-event-wordcamp.php) only if the file exists, otherwise loads default template.

    <?php
    function add_posttype_slug_template( $single_template ) {
    	$object                            = get_queried_object();
    	$single_posttype_postname_template = locate_template( "single-{$object->post_type}-{$object->post_name}.php" );
    
    	if ( file_exists( $single_posttype_postname_template ) ) {
    		return $single_posttype_postname_template;
    	} else {
    		return $single_template;
    	}
    }
    add_filter( 'single_template', 'add_postType_slug_template', 10, 1 );
    ?>
    

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