get_term_by()通过Term字段和数据获取所有Term信息

目录

描述

译文

通过Term字段和数据从数据库中获取所有Term资料。

警告:'name' $field中包括$value。必需时需要手动删除。

默认$field为'id',因此字段值也可能为空,不推荐使用空值。

$value不存在时,返回的值为假。$taxonomy存在且$field和$value组合存在时,Term被返回。

原文

Get all Term data from database by Term field and data.

Warning: $value is not HTML-escaped for the 'name' $field. You must do it yourself, if required.

The default $field is 'id', therefore, it is possible to also use null for field, but not recommended that you do so.

If $value does not exist, the return value will be false. If $taxonomy exists and $field and $value combinations exist, the Term will be returned.

get_term_by() 描述

用法

<?php get_term_by$field$value$taxonomy$output$filter ?>

get_term_by() 用法

参数

$field

(string) (必填) Either 'id', 'slug', 'name', or 'term_taxonomy_id'

默认值: 'id'

$value

(string|integer) (必填) Search for this term value

默认值: None

$taxonomy

(string) (必填) Taxonomy Name category, post_tag, link_category, nav_menu or something custom

默认值: None

$output

(string) (可选) Constant OBJECT, ARRAY_A, or ARRAY_N

默认值: OBJECT

$filter

(string) (可选) default is raw or no WordPress defined filter will applied.

默认值: 'raw'

get_term_by() 参数

返回值

(mixed) 

Term Row (object or array) from database. Will return false if $taxonomy does not exist or $term was not found. Othewise returns object (by default) or array of fields depending on $output parameter.

The fields returned are:

  • term_id (See warning below)
  • 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_term_by() 返回值

示例

Taxonomy_name is the name of taxonomy, not the term_name and is required; the id (term_id) is the ID of the term, not post_id;...

Remember:
↓ Taxonomy type (e.g. post_tag)
Terms in this taxonomy:
→ news
→ webdev
→ ...

Examples to get terms by name and taxonomy type (taxonomy_name as category, post_tag or custom taxonomy).

By id (term_id, not post_id):

Wrong example in this page history

Warning: the example below is wrong (see in this page history):

This example try to get a term with ID (term_id) as post_id and in the taxonomy taxonomy_name. This taxonomy does not exist and the term_id is wrong.

This is the correct version of this example:

get_term_by() 示例

注意

  • Warning: $value is not escaped for 'name' $field. You must do it yourself, if required.
  • See sanitize_term_field() The $context param lists the available values for 'get_term_by' $filter param.
  • 使用到: sanitize_term() Cleanses the term based on $filter context before returning.
  • 使用到 global: (object) $wpdb

get_term_by() 注意

历史

添加于 版本: 2.3.0

get_term_by() 历史

源文件

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

get_term_by() 源文件

相关