描述
如果指定了$taxonomy参数,函数将检查当前查询是否为所指定$taxonomy的自定义分类法归档。
此外,如果还指定了$term参数,则函数还将检查当前查询是否为所指定的$term归档。
更多类似的主题函数信息,请查看主题开发手册中的条件标签文章。
参数
- $taxonomy
-
(string|string[]) (可选) 一个或多个分类法slug(taxonomy slug)
默认值: ''
- $term
-
(int|string|int[]|string[]) (可选) 分类法项目ID(term ID)、name、slug或其数组。
默认值: ''
返回
(bool) 当前查询是否针对现有的自定义分类法归档页面。对于自定义分类法归档页面为True,对于内置分类法(category和tag归档)为false。
源码
更新日志
版本 | 描述 |
---|---|
2.5.0 | 开始引入 |
使用示例
例子
is_tax(); // 当任意自定义分类法归档页被显示时 is_tax( 'channel' ); // 当'channel'分类法的归档页被显示时
is_tax( 'channel', 'BBC1' ); // 当'channel'分类法的归档页被显示 // 并且'channel'分类法项目是'BBC1'$term参数也接受term对象。
is_tax( 'custom_taxonomy', $taxonomy_term_object );
如何限制特定分类法页面中显示的文章数量:
如果要创建基于自定义分类法而非类别(category)或标签(tag)的归档页,可以在
functions.php
文件中加入以下代码:add_action( 'pre_get_posts', function( $query) { if ( $query->is_tax( 'NAME_OF_TAXONOMY' ) ) { // Replace with the name of the taxonomy you want to target $query->set( 'posts_per_page', 6 ); // 显示文章数量6 } } );
要检查多个分类法,可以使用数组。当您只想在分类页面上显示一些代码时,这很有用。
if ( is_tax( array('channel', 'broadcaster') ) ) { // your code goes here }