Function Reference/get terms

来自WordPress中文文档
跳转至: 导航搜索

Description

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.

Usage

%%%<?php get_terms( $taxonomies, $args ) ?>%%%

Default Usage

<?php 
// no default values. using these as examples
$taxonomies = array( 
    'post_tag',
    'my_tax',
);

$args = array(
    'orderby'       => 'name', 
    'order'         => 'ASC',
    'hide_empty'    => true, 
    'exclude'       => array(), 
    'exclude_tree'  => array(), 
    'include'       => array(),
    'number'        => '', 
    'fields'        => 'all', 
    'slug'          => '', 
    'parent'         => '',
    'hierarchical'  => true, 
    'child_of'      => 0, 
    'get'           => '', 
    'name__like'    => '',
    'pad_counts'    => false, 
    'offset'        => '', 
    'search'        => '', 
    'cache_domain'  => 'core'
); 
?>

Parameters

tt$taxonomies/tt
(string|array) (required) The taxonomies to retrieve terms from.
Default: Nonenoinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

tt$args/tt
(string|array) (optional) Change what is returned.
Default: arraynoinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

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 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) An array of parent term ids to exclude
include 
(integer) 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
slug 
(string) 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.
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 'term_name%' query. This matches terms that begin with the 'name__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.
search 
(string) The term name you wish to match. It does a LIKE '%term_name%' query. This matches terms that contain the 'search' 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.

NOTE: The difference between 'search' and 'name__like' is the leading '%' in the LIKE clause. So search is '%search%' and name__like is 'name__like%'

Return Values

(array|WP_Error) 
Array of term objects or an empty array if no terms were found. WP_Error if any of $taxonomies does not exist.

Examples

Get all post categories ordered by count.

String syntax:

$categories = get_terms( 'category', 'orderby=count&hide_empty=0' );

Array syntax:

 $categories = get_terms( 'category', array(
 	'orderby'    => 'count',
 	'hide_empty' => 0
 ) );

Get all the links categories:

$mylinks_categories = get_terms('link_category', 'orderby=count&hide_empty=0');

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

 $terms = get_terms("my_taxonomy");
 $count = count($terms);
 if ( $count > 0 ){
     echo "<ul>";
     foreach ( $terms as $term ) {
       echo "<li>" . $term->name . "</li>";
        
     }
     echo "</ul>";
 }

List all the terms, with link to term archive, separated by an interpunct (·). (language specific, WPML method):

$args = array( 'taxonomy' => 'my_term' );

$terms = get_terms('my_term', $args);

$count = count($terms); $i=0;
if ($count > 0) {
    $cape_list = '<p class="my_term-archive">';
    foreach ($terms as $term) {
        $i++;
    	$term_list .= '<a href="/term-base/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>';
    	if ($count != $i) $term_list .= ' &middot; '; else $term_list .= '</p>';
    }
    echo $term_list;
}

Details

You can inject any customizations to the query before it is sent or control the output with filters.

The 'get_terms' filter will be called when the cache has the term and will pass the found term along with the array of $taxonomies and array of $args.

This filter is also called before the array of terms is passed and will pass the array of terms, along with the $taxonomies and $args.

The 'list_terms_exclusions' filter passes the compiled exclusions along with the $args.

Source File

get_terms() is located in onlyincludecodewp-includes/taxonomy.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on http://core.trac.wordpress.org/browser/.

Parameters

  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.

Usage

Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre

/div

wordpress.org.cn.

Related

Terms: is_term, term_exists, get_term, get_term_by, get_term_children, get_terms, sanitize term, wp_get_object_terms

Resources

includeonlydiv style=clear:both; background-color:#F7F7F7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;See also index of Function Reference and index of Template Tags./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude