WordPress函数taxonomy_exists()确定分类名称是否存在

WordPress函数is_taxonomy_hierarchical()确定分类对象是否分层

WordPress函数is_taxonomy_hierarchical()确定分类对象是否分层

  WordPress函数is_taxonomy_hierarchical()首先进行检查以确保分类法是一个对象。然后获取对象,最后返回对象中的层次值。

  错误的返回值也可能意味着分类不存在。

  推荐:[最新版]Slider Revolution滑块插件WordPress滑块插件

is_taxonomy_hierarchical()函数基本语法

描述

  确定分类对象是否分层

用法

is_taxonomy_hierarchical( $taxonomy );

参数

  • $taxonomy (字符串)(必需)分类对象的名称。

  用法示例:

$taxonomy = 'category';
$is_hierarchical = is_taxonomy_hierarchical( $taxonomy );
echo $is_hierarchical; // Output: true

  推荐:[最新版]TranslatePress Pro插件下载WordPress翻译插件

is_taxonomy_hierarchical()函数

  is_taxonomy_hierarchical()确定分类对象是否分层(源文件可参考这里

function is_taxonomy_hierarchical( $taxonomy ) {
	if ( ! taxonomy_exists( $taxonomy ) ) {
		return false;
	}

	$taxonomy = get_taxonomy( $taxonomy );
	return $taxonomy->hierarchical;
}

  推荐:[最新版]LearnDash LMS插件WordPress学习管理系统插件

如何使用is_taxonomy_hierarchical()

  检查自定义分类法是否是分层的

$taxonomy = 'custom_taxonomy';
$is_hierarchical = is_taxonomy_hierarchical( $taxonomy );

if ( $is_hierarchical ) {
    echo 'This taxonomy is hierarchical.';
} else {
    echo 'This taxonomy is not hierarchical.';
}

  显示层次分类法

$taxonomies = get_taxonomies();

foreach ( $taxonomies as $taxonomy ) {
    if ( is_taxonomy_hierarchical( $taxonomy ) ) {
        echo $taxonomy . ' is hierarchical.<br>';
    }
}

  添加分层分类的自定义样式

$taxonomy = 'custom_taxonomy';
$style = '';

if ( is_taxonomy_hierarchical( $taxonomy ) ) {
    $style = 'hierarchical-style';
}

echo '<div class="' . $style . '">Taxonomy Content</div>';

  仅显示分层分类法的父术语

$taxonomy = 'custom_taxonomy';

if ( is_taxonomy_hierarchical( $taxonomy ) ) {
    $terms = get_terms( array(
        'taxonomy'   => $taxonomy,
        'parent'     => 0,
    ) );

    foreach ( $terms as $term ) {
        echo $term->name . '<br>';
    }
}

  检查帖子类型是否支持分层分类法

$post_type = 'post';
$taxonomies = get_object_taxonomies( $post_type );
$hierarchical_taxonomies = array();

foreach ( $taxonomies as $taxonomy ) {
    if ( is_taxonomy_hierarchical( $taxonomy ) ) {
        $hierarchical_taxonomies[] = $taxonomy;
    }
}

if ( !empty( $hierarchical_taxonomies ) ) {
    echo 'The ' . $post_type . ' post type supports the following hierarchical taxonomies:';
    echo implode( ', ', $hierarchical_taxonomies );
} else {
    echo 'The ' . $post_type . ' post type does not support any hierarchical taxonomies.';
}

  推荐:WordPress函数get_theme_support()获取注册支持时传递的主题参数

  推荐:WordPress函数使用手册

5/5 - (1 vote)

Claude、Netflix、Midjourney、ChatGPT Plus、PS、Disney、Youtube、Office 365、多邻国Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折

Chatgpt-Plus注册购买共享账号
滚动至顶部