目录
描述
译文
通过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.
用法
<?php get_term_by( $field, $value, $taxonomy, $output, $filter ) ?>
参数
$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'
返回值
(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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ // Get term by name ''news'' in Categories taxonomy. $category = get_term_by('name', 'news', 'category') // Get term by name ''news'' in Tags taxonomy. $tag = get_term_by('name', 'news', 'post_tag') // Get term by name ''news'' in Custom taxonomy. $term = get_term_by('name', 'news', 'my_custom_taxonomy') // Get term by name ''Default Menu'' from theme's nav menus. // (Alternative to using wp_get_nav_menu_items) $menu = get_term_by('name', 'Default Menu', 'nav_menu'); |
By id (term_id, not post_id):
1 2 3 4 5 6 7 8 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ // Get term by id (''term_id'') in Categories taxonomy. get_term_by('id', 12, 'category') ... |
Wrong example in this page history
Warning: the example below is wrong (see in this page history):
1 2 3 4 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ get_term_by('id', (int)$post->ID, 'taxonomy_name'); // return null |
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:
1 2 3 4 5 6 7 8 9 10 11 12 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ // get_term_by('id', category_id, 'category') $postCategories = get_the_category($post->ID); foreach ( $postCategories as $postCategory ) { $myCategories[] = get_term_by('id', $postCategory->cat_ID, 'category'); } // OR: $myCategory = get_term_by('id', $postCategories[0]->cat_ID, 'category'); |
注意
- 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
历史
添加于 版本: 2.3.0
源文件
get_term_by() 函数的代码位于 wp-includes/taxonomy.php
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ /** * Get all Term data from database by Term field and data. * * Warning: $value is not escaped for '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. * * @todo Better formatting for DocBlock. * * @since 2.3.0 * * @global wpdb $wpdb WordPress database abstraction object. * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param. * * @param string $field Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id' * @param string|int $value Search for this term value * @param string $taxonomy Taxonomy Name * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N * @param string $filter Optional, default is raw or no WordPress defined filter will applied. * @return object|array|null|WP_Error|false Term Row from database. * Will return false if $taxonomy does not exist or $term was not found. */ function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') { global $wpdb; if ( ! taxonomy_exists($taxonomy) ) return false; if ( 'slug' == $field ) { $field = 't.slug'; $value = sanitize_title($value); if ( empty($value) ) return false; } elseif ( 'name' == $field ) { // Assume already escaped $value = wp_unslash($value); $field = 't.name'; } elseif ( 'term_taxonomy_id' == $field ) { $value = (int) $value; $field = 'tt.term_taxonomy_id'; } else { $term = get_term( (int) $value, $taxonomy, $output, $filter ); if ( is_wp_error( $term ) ) $term = false; return $term; } $term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND $field = %s LIMIT 1", $taxonomy, $value ) ); if ( ! $term ) return false; wp_cache_add( $term->term_id, $term, $taxonomy ); /** This filter is documented in wp-includes/taxonomy.php */ $term = apply_filters( 'get_term', $term, $taxonomy ); /** This filter is documented in wp-includes/taxonomy.php */ $term = apply_filters( "get_$taxonomy", $term, $taxonomy ); $term = sanitize_term($term, $taxonomy, $filter); if ( $output == OBJECT ) { return $term; } elseif ( $output == ARRAY_A ) { return get_object_vars($term); } elseif ( $output == ARRAY_N ) { return array_values(get_object_vars($term)); } else { return $term; } } |
get_term_by() 源文件
- 原文:http://codex.wordpress.org/Function_Reference/get_term_by
- 翻译:黄聪@WordPress之魂