函数文档

taxonomy_exists()

💡 云策文档标注

概述

taxonomy_exists() 函数用于检查指定的分类法名称是否存在。它替代了已弃用的 is_taxonomy() 函数,自 WordPress 2.3.0 版本引入。

关键要点

  • 函数接受一个字符串参数 $taxonomy,表示分类法对象的名称。
  • 返回布尔值,如果分类法存在则返回 true,否则返回 false。
  • 内部实现通过检查全局变量 $wp_taxonomies 中是否存在对应键值来判断。
  • 相关函数包括 unregister_taxonomy()、get_taxonomy() 等,用于分类法管理操作。

代码示例

$taxonomy_exist = taxonomy_exists( 'category' );
// 返回 true

$taxonomy_exist = taxonomy_exists( 'post_tag' );
// 返回 true

$taxonomy_exist = taxonomy_exists( 'link_category' );
// 返回 true

$taxonomy_exist = taxonomy_exists( 'my_taxonomy' );
// 如果全局 $wp_taxonomies['my_taxonomy'] 未设置,则返回 false

📄 原文内容

Determines whether the taxonomy name exists.

Description

Formerly is_taxonomy() , introduced in 2.3.0.

For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.

Parameters

$taxonomystringrequired
Name of taxonomy object.

Return

bool Whether the taxonomy exists.

Source

function taxonomy_exists( $taxonomy ) {
	global $wp_taxonomies;

	return is_string( $taxonomy ) && isset( $wp_taxonomies[ $taxonomy ] );
}

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Basic Example

    $taxonomy_exist = taxonomy_exists( 'category' );
    // Returns true
    
    $taxonomy_exist = taxonomy_exists( 'post_tag' );
    // Returns true
    
    $taxonomy_exist = taxonomy_exists( 'link_category' );
    // Returns true
    
    $taxonomy_exist = taxonomy_exists( 'my_taxonomy' );
    // Returns false if global $wp_taxonomies['my_taxonomy'] is not set