Wordpress:如何在taxonomy.php 中获取分类名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3376651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:36:55  来源:igfitidea点击:

Wordpress : How Do I get taxonomy name in taxonomy.php?

wordpresstaxonomy

提问by James

I am able to display the term of the taxonomy in the taxonomy page, but how do I get the taxonomy , or display the taxonomy on the page.

我可以在分类法页面中显示分类法的术语,但是如何获取分类法,或在页面上显示分类法。

for example, when I have a taxonomy called "fruit" and I click on a fruit term called "lemons", How do I display both "lemons" and "fruit" on the taxonomy term page?

例如,当我有一个名为“fruit”的分类并单击名为“lemons”的水果术语时,如何在分类术语页面上同时显示“lemons”和“fruit”?

Just looking for the get term equivalent. Thx!

只是在寻找等效的 get 术语。谢谢!

回答by Kiran Raj Pradhan

A possible solution:

一个可能的解决方案:

$taxonomy = get_queried_object();
echo  $taxonomy->name;

回答by chodorowicz

For my taste is overly complicated, but here it goes:

对于我的口味来说过于复杂,但它是这样的:

$term = get_term_by('slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
echo $term->name;

回答by Jan Fabry

If you check $wp_query->get_queried_object()on a taxonomy page, this will contain the term object, which has a reference to the taxonomy identifier (in my example it's replymc_people). Pass this to get_taxonomy, and you get the full taxonomy object.

如果您检查$wp_query->get_queried_object()分类法页面,这将包含术语对象,该对象具有对分类法标识符的引用(在我的示例中为replymc_people)。将此传递给get_taxonomy,您将获得完整的分类法对象。

object(stdClass)[325]
  public 'term_id' => string '113' (length=3)
  public 'name' => string 'Jef Staes' (length=9)
  public 'slug' => string 'jef-staes' (length=9)
  public 'term_group' => string '0' (length=1)
  public 'term_taxonomy_id' => string '107' (length=3)
  public 'taxonomy' => string 'replymc_people' (length=14)
  public 'description' => string '' (length=0)
  public 'parent' => string '0' (length=1)
  public 'count' => string '3' (length=1)

回答by Victor Teixeira

get_query_var('taxonomy');

Just this should work.

这应该工作。

回答by DeviousBlue

I know this is a solved problem but here's another way to get the taxonomy name which I think is clean. For those finding this just now as I did. I like to promote awareness of global variables in wordpress.

我知道这是一个已解决的问题,但这是获得我认为干净的分类名称的另一种方法。对于那些像我一样刚刚发现这一点的人。我喜欢在 wordpress 中提高对全局变量的认识。

$tax_term = $wp_query->query_vars['tax_name'];

回答by gilzow

I know this is answered, but for those who might wind up on this page when searching...

我知道这已经得到了回答,但是对于那些在搜索时可能会出现在此页面上的人...

global $taxonomy,$term;

$taxonomy will now contain your taxonomy name ('fruit' from the OP's example) and your taxonomy term name ('lemon' from OP's example).

$taxonomy 现在将包含您的分类名称(来自 OP 示例中的“fruit”)和您的分类术语名称(来自 OP 示例中的“lemon”)。

回答by Shashank Sharma

You can get specific term / taxonomy data like name, description etc on taxonomy.php

您可以在 taxonomy.php 上获取特定的术语/分类数据,如名称、描述等

<?php 
$termid = get_queried_object()->term_id;
$term = get_term( $termid, 'taxonomy-name' );
echo $term->name.'<br>';
echo $term->description;
?>

回答by Chris Pink

Someone might step this way (I did) so...

有人可能会这样(我这样做)所以......

$tax = $wp_query->get_queried_object();
$tax_term = $tax->name;
$tax = get_taxonomy($tax->taxonomy);
$taxonomy =  $tax->label;
echo "$tax_term are $taxonomy";

returns "Lemons are Fruit".

返回“柠檬是水果”。

I'd love to know a one liner for this but I don't think there is one, you need parts of two objects.

我很想知道一个单衬,但我认为没有,你需要两个物体的一部分。