get_terms()获取某个分类法下的所有分类信息

目录

描述

译文

检索分类法或分类法列表中的term。

原文

Retrieve the terms in a taxonomy or list of taxonomies.

Returns an array of term objects, or a WP_Error object if any of the taxonomies to get terms from does not exist.

get_terms() 描述

用法

<?php get_terms$taxonomies$args ); ?>

Default Usage

get_terms() 用法

参数

$taxonomies

(string|array) (必填) The taxonomies to retrieve terms from. (Default Taxonomies)

默认值: None

$args

(string|array) (可选) Change what is returned.

默认值: array

Possible Arguments

orderby 

(string)

  • id
  • count
  • name - Default
  • slug
  • term_group - Not fully implemented (avoid using)
  • none

order 

(string)

  • ASC - Default
  • DESC

hide_empty 

(boolean) Whether to not return empty $terms.

  • 1 (true) - Default (i.e. Do not show empty terms)
  • 0 (false)

exclude 

(integer|string|array) An array of term ids to exclude. Also accepts a string of comma-separated ids.

exclude_tree 

(integer|string|array) An array of parent term ids to exclude

include 

(integer|string|array) An array of term ids to include. Empty returns all.

number 

(integer) The maximum number of terms to return. Default is to return them all.

fields 

(string)

  • all - returns an array of term objects - Default
  • ids - returns an array of integers
  • names - returns an array of strings
  • count - (3.2+) returns the number of terms found
  • id=>parent - returns an associative array where the key is the term id and the value is the parent term id if present or 0
  • id=>slug - returns an associative array where the key is the term id and the value is the slug
  • id=>name - returns an associative array where the key is the term id and the value is the name

slug 

(string|array) Returns terms whose "slug" matches this value. Default is empty string.

parent 

(integer) Get direct children of this term (only terms whose explicit parent is this value). If 0 is passed, only top-level terms are returned. Default is an empty string.

hierarchical 

(boolean) Whether to include terms that have non-empty descendants (even if 'hide_empty' is set to true).

  • 1 (true) - Default
  • 0 (false)

child_of 

(integer) Get all descendents of this term. Default is 0. Note: the difference between child_of and parent is that where parent only gets direct children of the parent term (ie: 1 level down), child_of gets all descendants (as many levels as are available)

childless 

(boolean) Returns terms that have no children if taxonomy is hierarchical, all terms if taxonomy is not hierarchical

get 

(string) Default is nothing . Allow for overwriting 'hide_empty' and 'child_of', which can be done by setting the value to 'all'.

name__like 

(string) The term name you wish to match. It does a LIKE '%string%' query against the term name only. This matches terms that contain the 'name__like' string. Note: This was changed in WordPress 3.7, when previously name__like matched terms that begin with the string. See ticket #8214.

description__like 

(string) Returned terms' descriptions will contain the value of 'description__like', case-insensitive. Default is empty string. It does a LIKE '%string%' query against the term description only. This matches terms that contain the 'description__like' string.

pad_counts 

(boolean) If true, count all of the children along with the $terms.

  • 1 (true)
  • 0 (false) - Default

offset 

(integer) The number by which to offset the terms query. Must be used in conjunction with 'number' otherwise 'offset' is ignored and the entire result set is returned.

search 

(string) The term name you wish to match. It does a LIKE '%search_string%' query against the term name and slug. This matches terms that contain the 'search_string' string.

cache_domain 

(string) Version 3.2 and above. The 'cache_domain' argument enables a unique cache key to be produced when the query produced by get_terms() is stored in object cache. For instance, if you are using one of this function's filters to modify the query (such as 'terms_clauses'), setting 'cache_domain' to a unique value will not overwrite the cache for similar queries. Default value is 'core'.

NOTE: Arguments are passed in the format used by wp_parse_args(). e.g.

get_terms() 参数

返回值

(array|string|WP_Error) 

Array of term objects or an empty array if no terms were found. WP_Error if any of $taxonomies does not exist. If the 'fields' argument was 'count', the number of terms found will be returned as a string.

The fields returned are:

  • term_id
  • name
  • slug
  • term_group
  • term_taxonomy_id
  • taxonomy
  • description
  • parent
  • count

Warning: string vs integer confusion! Field values, including term_id are returned in string format. Before further use, typecast numeric values to actual integers, otherwise WordPress will mix up term_ids and slugs which happen to have only numeric characters

get_terms() 返回值

示例

Get all post categories ordered by count.

String syntax:

Array syntax:

Get all the links categories:

List all the terms in a custom taxonomy, without a link:

List all the terms, with link to term archive, separated by an interpunct (·):

get_terms() 示例

历史

  • 添加于 版本: 2.3.0

get_terms() 历史

源文件

get_terms() 函数的代码位于 wp-includes/taxonomy.php.

get_terms() 源文件

相关