WordPress函数get_terms()检索分类法或分类法列表中的term

WordPress函数get_terms()检索分类法或分类法列表中的term

WordPress函数get_terms()检索分类法或分类法列表中的term

  WordPress 中的函数get_terms()功能是一个强大的工具,用于检索分类法或分类法列表中的术语。如果您正在处理网站上的类别、标签或自定义分类法,

  推荐:[最新版]WPForms Pro插件WordPress表单生成器

get_terms()函数基本语法

$terms = get_terms( array(
    'taxonomy' => 'category',
    'hide_empty' => false,
) );

  get_terms()函数具有三个可选参数,允许您进一步自定义其输出:

  • taxonomy (string|array) – 分类名称或分类名称数组,结果应限制在其中。
  • object_ids (int|array) – 对象 ID 或对象 ID 数组。结果将仅限于与这些对象相关的术语。
  • orderby (string) – 用于对术语进行排序的字段。
  • order (string) – 是否按升序或降序对术语进行排序。接受“ASC”(升序)或“DESC”(降序)。默认“ASC”。
  • hide_empty (bool|int) – 是否隐藏未分配给任何帖子的术语。接受 1|true 或 0|false。默认 1|true。
  • include (int|string) – 要包含的术语 ID 的数组或逗号/空格分隔的字符串。默认空数组。
  • except (int|string) – 要排除的术语 ID 的数组或逗号/空格分隔的字符串。如果 $include 非空,则忽略 $exclude。默认空数组。
  • except_tree (int|string) – 要排除的术语 ID 及其所有后代术语的数组或逗号/空格分隔字符串。如果 $include 非空,则忽略 $exclude_tree。

  推荐:[最新版]Shoptimizer主题下载最快的WooCommerce主题

get_terms()函数

  WordPress 中的 get_terms() 函数用于从指定的分类中检索术语。常见的分类法包括“category”、“post_tag”等。它返回与传递给函数的参数匹配的术语对象数组。(源文件可参考这里

function get_terms( $args = array(), $deprecated = '' ) {
	$term_query = new WP_Term_Query();

	$defaults = array(
		'suppress_filter' => false,
	);

	/*
	 * Legacy argument format ($taxonomy, $args) takes precedence.
	 *
	 * We detect legacy argument format by checking if
	 * (a) a second non-empty parameter is passed, or
	 * (b) the first parameter shares no keys with the default array (ie, it's a list of taxonomies)
	 */
	$_args          = wp_parse_args( $args );
	$key_intersect  = array_intersect_key( $term_query->query_var_defaults, (array) $_args );
	$do_legacy_args = $deprecated || empty( $key_intersect );

	if ( $do_legacy_args ) {
		$taxonomies       = (array) $args;
		$args             = wp_parse_args( $deprecated, $defaults );
		$args['taxonomy'] = $taxonomies;
	} else {
		$args = wp_parse_args( $args, $defaults );
		if ( isset( $args['taxonomy'] ) && null !== $args['taxonomy'] ) {
			$args['taxonomy'] = (array) $args['taxonomy'];
		}
	}

	if ( ! empty( $args['taxonomy'] ) ) {
		foreach ( $args['taxonomy'] as $taxonomy ) {
			if ( ! taxonomy_exists( $taxonomy ) ) {
				return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
			}
		}
	}

	// Don't pass suppress_filter to WP_Term_Query.
	$suppress_filter = $args['suppress_filter'];
	unset( $args['suppress_filter'] );

	$terms = $term_query->query( $args );

	// Count queries are not filtered, for legacy reasons.
	if ( ! is_array( $terms ) ) {
		return $terms;
	}

	if ( $suppress_filter ) {
		return $terms;
	}

	/**
	 * Filters the found terms.
	 *
	 * @since 2.3.0
	 * @since 4.6.0 Added the `$term_query` parameter.
	 *
	 * @param array         $terms      Array of found terms.
	 * @param array|null    $taxonomies An array of taxonomies if known.
	 * @param array         $args       An array of get_terms() arguments.
	 * @param WP_Term_Query $term_query The WP_Term_Query object.
	 */
	return apply_filters( 'get_terms', $terms, $term_query->query_vars['taxonomy'], $term_query->query_vars, $term_query );
}

  推荐:ProjectHuddle插件WordPress网站设计交流插件

如何使用get_terms()函数

  获取所有类别,获取所有类别而不隐藏空类别。

$categories = get_terms( array(
    'taxonomy' => 'category',
    'hide_empty' => false,
) );

  从多个分类法中获取术语,获取“category”和“post_tag”分类法中的所有术语。

$terms = get_terms( array( 
    'taxonomy' => array('category', 'post_tag'), 
    'hide_empty' => false, 
) );

  显示页面上的所有分类,下面是一个检索所有类别并回显其名称的片段:

$categories = get_terms( array(
    'taxonomy' => 'category',
    'hide_empty' => false
) );

if ( !empty($categories) ) {
    echo '<ul>';
    foreach ( $categories as $category ) {
        echo '<li>' . $category->name . '</li>';
    }
    echo '</ul>';
}

  获取特定类别的子类别,假设您有一个名为“Books”的类别,并且它有多个子类别,如“Fiction”、“Non-Fiction”、“Biographies”等。要仅获取“Books”的子类别,您可以使用参数指定其'parent'ID :

$book_subcategories = get_terms( array(
    'taxonomy' => 'category',
    'parent' => 10, // Replace with the ID of 'Books' category
    'hide_empty' => false
) );

if ( !empty($book_subcategories) ) {
    echo '<ul>';
    foreach ( $book_subcategories as $category ) {
        echo '<li>' . $category->name . '</li>';
    }
    echo '</ul>';
}

  从自定义分类中获取术语,如果您有一个名为“流派”的自定义分类法,用于自定义帖子类型“电影”,则可以按如下方式获取所有术语:

$genres = get_terms( array(
    'taxonomy' => 'genre',
    'hide_empty' => false
) );

if ( !empty($genres) ) {
    echo '<ul>';
    foreach ( $genres as $genre ) {
        echo '<li>' . $genre->name . '</li>';
    }
    echo '</ul>';
}

  推荐:WordPress函数wp_check_filetype()检索文件类型

  推荐:WordPress函数使用手册


滚动至顶部