如何从分类中获得分类学术语名称?
-
-
您是否要创建链接,标题,???are you wanting to create a link, title, ???
- 0
- 2011-05-05
- xLRDxREVENGEx
-
3 个回答
- 投票数
-
- 2011-05-05
您要查找的功能是
get_term_by
.您可以这样使用它:<?php $term = get_term_by('slug', 'my-term-slug', 'category'); $name = $term->name; ?>
这导致
$term
是包含以下内容的对象:term_id name slug term_group term_taxonomy_id taxonomy description parent count
该法典在解释此功能方面做得非常出色: http://codex.wordpress.org/Function_Reference/get_term_by
The function you are looking for is
get_term_by
. You would use it as such:<?php $term = get_term_by('slug', 'my-term-slug', 'category'); $name = $term->name; ?>
This results in
$term
being an object containing the following:term_id name slug term_group term_taxonomy_id taxonomy description parent count
The codex does a great job explaining this function: http://codex.wordpress.org/Function_Reference/get_term_by
-
你击败了我.这正是我要做的.you beat me to it. This is exactly what i would do to.
- 0
- 2011-05-05
- xLRDxREVENGEx
-
如果没有分类标准怎么办?What if you don't have the taxonomy slug?
- 1
- 2017-05-07
- EkoJR
-
如果您只有ID,则可以使用`get_term($term_id);`.You can use `get_term( $term_id );` if you only have the ID.
- 0
- 2020-07-11
- Gavin
-
- 2017-05-07
当分类法不可用/未知时,这提供了一个答案.
就我而言,当使用get_term_by 时,在某些情况下仅仅是术语子弹(没有术语ID或分类法).导致我来到这里.但是,提供的答案并不能完全解决我的问题.
空的
的解决方案$taxonomy
// We want to find the ID to this slug. $term_slug = 'foo-bar'; $taxonomies = get_taxonomies(); foreach ( $taxonomies as $tax_type_key => $taxonomy ) { // If term object is returned, break out of loop. (Returns false if there's no object) if ( $term_object = get_term_by( 'slug', $term_slug , $taxonomy ) ) { break; } } $term_id = $term_object->name; echo 'The Term ID is: ' . $term_id . '<br>'; var_dump( $term_object );
结果
The Term ID is: 32 object(WP_Term) public 'term_id' => int 32 public 'name' => string 'Example Term' public 'slug' => string 'example-term' public 'term_group' => int 0 public 'term_taxonomy_id' => int 123 public 'taxonomy' => string 'category' public 'description' => string '' public 'parent' => int 0 public 'count' => int 23 public 'filter' => string 'raw'
如下所示,该概念获取一个
$taxonomies
数组,循环遍历该数组,如果get_term_by()
返回一个匹配项,则立即中断该匹配foreach循环.注意:我尝试搜索一种方法来从Term Slug获取关联的分类法(ID或Slug),但是很遗憾,我无法在WordPress中找到可用的任何内容.
This provides an answer when the taxonomy is unavailable/unknown.
In my case, when using get_term_by, there were some instances where there was only the Term Slug ( No Term ID or Taxonomy ). Which led me here. However, the answer provided didn't quite resolve my issue.
Solution for empty
$taxonomy
// We want to find the ID to this slug. $term_slug = 'foo-bar'; $taxonomies = get_taxonomies(); foreach ( $taxonomies as $tax_type_key => $taxonomy ) { // If term object is returned, break out of loop. (Returns false if there's no object) if ( $term_object = get_term_by( 'slug', $term_slug , $taxonomy ) ) { break; } } $term_id = $term_object->name; echo 'The Term ID is: ' . $term_id . '<br>'; var_dump( $term_object );
Result
The Term ID is: 32 object(WP_Term) public 'term_id' => int 32 public 'name' => string 'Example Term' public 'slug' => string 'example-term' public 'term_group' => int 0 public 'term_taxonomy_id' => int 123 public 'taxonomy' => string 'category' public 'description' => string '' public 'parent' => int 0 public 'count' => int 23 public 'filter' => string 'raw'
As follows, the concept gets an array of
$taxonomies
, loops through the array, and IFget_term_by()
returns a match, it then immediately breaks out of the foreach loop.Note: I tried searching for a method to get the associated taxonomy ( ID or Slug ) from Term Slug, but unfortunately I am unable to find anything available in WordPress.
-
- 2019-01-03
谢谢,这对我有用.
我创建了一个函数,并根据需要反复使用它.
function helper_get_taxonomy__by_slug($term_slug){ $term_object = ""; $taxonomies = get_taxonomies(); foreach ($taxonomies as $tax_type_key => $taxonomy) { // If term object is returned, break out of loop. (Returns false if there's no object); if ($term_object = get_term_by('slug', $term_slug, $taxonomy)) { break; }else{ $term_object = "Warn! Helper taxonomy not found."; } } return $term_object; }
thanks, this worked for me.
I created a function and use it again and again as needed.
function helper_get_taxonomy__by_slug($term_slug){ $term_object = ""; $taxonomies = get_taxonomies(); foreach ($taxonomies as $tax_type_key => $taxonomy) { // If term object is returned, break out of loop. (Returns false if there's no object); if ($term_object = get_term_by('slug', $term_slug, $taxonomy)) { break; }else{ $term_object = "Warn! Helper taxonomy not found."; } } return $term_object; }
-
成功时,您应该返回与get_term_by相同的类型:(WP_Term| array| false)WP_Term实例(或数组).如果不存在$taxonomy或未找到$term,则将返回false.You should return the same types as get_term_by: (WP_Term|array|false) WP_Term instance (or array) on success. Will return false if $taxonomy does not exist or $term was not found.
- 0
- 2020-05-25
- xnagyg
如果我知道一个分类学术语,我该如何获得该术语的名称?