当前浏览:首页 / WordPress钩子 / {$type}_template

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

按类型过滤查询模板的路径

templatemore...

typemore...


描述

钩子名称的动态部分$type指的是要加载的文件的文件名 - 去除文件扩展名和任何非字母数字字符定界词。此钩子还适用于作为模板层次结构的一部分加载的各种类型的文件。

可能的挂钩名称包括:

  • 404_template
  • archive_template
  • attachment_template
  • author_template
  • category_template
  • date_template
  • embed_template
  • frontpage_template
  • home_template
  • index_template
  • page_template
  • paged_template
  • privacypolicy_template
  • search_template
  • single_template
  • singular_template
  • tag_template
  • taxonomy_template

参数

$template

(string) 模板的路径。参见locate_template()

$type

(string) 无扩展名的已清理文件名。

$templates

(string[]) 按优先级降序排列的模板候选列表。


更多信息

如果您需要对WordPress的模板选择和加载系统进行更精细的控制,请考虑使用template_include



源码

查看源码 官方文档


更新日志

版本描述
4.8.0添加了$type$templates参数。
1.5.0开始引入

使用示例

  • 示例1

    如果我们想为从插件创建的自定义分类添加并存档页面,我们可以使用此过滤器。在这种情况下,我们需要的过滤器是“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

    从Codex迁移的示例:

    示例代码将加载插件文件夹中的模板文件“post-type-template.php”,用于任何类型为“my_post_type”的文章或页面,否则将使用默认模板。

    <?php
    add_filter( 'single_template', 'get_custom_post_type_template' );
    
    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;
    }
    ?>
    
  • 示例3

    从Codex迁移的示例:

    此示例仅在模板文件single-{post_type}-{slug}.php(例如single-event-wordcamp.php)存在时加载该文件,否则加载默认模板。

    <?php
    add_filter( 'single_template', 'add_postType_slug_template', 10, 1 );
    
    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;
    	}
    }
    ?>
    
  • 示例4

    从Codex迁移的示例:

    此示例为类别62、65和59加载自定义类别模板。

    add_filter( 'category_template', 'filter_category_template', 99 );
    
    function filter_category_template( $template ) {
    
    	 if ( is_category(array( 64,65,59 )  )  ) {
    		$new_template = locate_template( array( 'category-template.php' ) );
    		if ( '' != $new_template ) {
    			return $new_template;
    		}
    	}
    
    	return $template;
    }
    
  • 示例5

    从Codex迁移的示例:

    下面的示例将为任何类型为“my_post_type”的存档页面加载插件文件夹中的模板文件“post-type-template.php”否则,将使用默认模板。

    add_filter( 'archive_template', 'get_custom_post_type_template' );
    
    function get_custom_post_type_template( $archive_template ) {
         global $post;
    
         if ( is_post_type_archive ( 'my_post_type' ) ) {
              $archive_template = dirname( __FILE__ ) . '/post-type-template.php';
         }
         return $archive_template;
    }