php 如何在 WordPress 上获取当前的分类术语 ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12289169/
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-08-25 03:14:53  来源:igfitidea点击:

How do I get current taxonomy term id on WordPress?

phpwordpresstaxonomy

提问by InvalidSyntax

I've created a taxonomy.php page in my WordPress theme folder. I would like to get the current term id for a function. How can I get this?

我在我的 WordPress 主题文件夹中创建了一个 taxonomy.php 页面。我想获取函数的当前术语 ID。我怎样才能得到这个?

get_query_var('taxonomy')only returns the term slug, I want the ID

get_query_var('taxonomy')只返回术语 slug,我想要 ID

回答by InvalidSyntax

Nevermind! I found it :)

没关系!我找到了 :)

get_queried_object()->term_id;

回答by theMukhiddin

Simple and easy!

简单易行!

get_queried_object_id()

回答by Tim Bowen

Here's the whole code snippet needed:

这是所需的整个代码片段:

$queried_object = get_queried_object();
$term_id = $queried_object->term_id;

回答by Varsha Dhadge

Just copy paste below code!

只需复制粘贴下面的代码!

This will print your current taxonomy name and description(optional)

这将打印您当前的分类名称和描述(可选)

<?php 
   $tax = $wp_query->get_queried_object();
   echo ''. $tax->name . '';
   echo "<br>";
   echo ''. $tax->description .''; 
?>

回答by Fury

If you are in taxonomy page.

如果您在分类页面。

That's how you get all details about the taxonomy.

这就是您获取有关分类法的所有详细信息的方式。

get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

This is how you get the taxonomy id

这就是您获得分类 ID 的方式

$termId = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) )->term_id;


But if you are in post page (taxomony -> child)

但是如果你在帖子页面(分类 -> 孩子)

$terms = wp_get_object_terms( get_queried_object_id(), 'taxonomy-name');
$term_id = $terms[0]->term_id;

回答by Jadson Moreira

<?php 
$terms = get_the_terms( $post->ID, 'taxonomy');
foreach ( $terms as $term ) {
    $termID[] = $term->term_id;
}
echo $termID[0]; 
?>

回答by Purvik Dhorajiya

See wp_get_post_terms(), you'd do something like so:

wp_get_post_terms(),你会做这样的事情:

global $post;
$terms = wp_get_post_terms( $post->ID, 'YOUR_TAXONOMY_NAME',array('fields' => 'ids') );

print_r($terms);

回答by SYED FARHAN KARIM

It's the term slug you want.Looks like you can get the id like this if that's what you need:

这是你想要的术语 slug.Looks like you can get the id like this if that is you need:

function get_term_link( $term, $taxonomy = '' ) {
    global $wp_rewrite;

    if ( !is_object($term) ) {
        if ( is_int( $term ) ) {
            $term = get_term( $term, $taxonomy );
        } else {
            $term = get_term_by( 'slug', $term, $taxonomy );
        }
    }