如何在wordpress上获得当前的分类术语id ?

时间:2022-11-26 07:28:25

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

我已经创建了一个分类。在我的wordpress模板文件夹中的php页面,我想获得一个函数的当前术语id。我怎么能得到这个呢?

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

get_query_var(“taxonomy”)只返回term slug,我需要ID

7 个解决方案

#1


219  

Nevermind! I found it :)

别介意!我发现它:)

get_queried_object()->term_id;

#2


32  

Here's the whole code snippet needed:

以下是所需的全部代码片段:

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

#3


17  

Simple and easy!

简单和容易的!

get_queried_object_id()

#4


4  

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

#5


4  

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 .''; 
?>

#6


1  

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);

#7


0  

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

这是你想要的术语。如果你需要的话,你可以得到这样的id:

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 );
        }
    }

#1


219  

Nevermind! I found it :)

别介意!我发现它:)

get_queried_object()->term_id;

#2


32  

Here's the whole code snippet needed:

以下是所需的全部代码片段:

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

#3


17  

Simple and easy!

简单和容易的!

get_queried_object_id()

#4


4  

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

#5


4  

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 .''; 
?>

#6


1  

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);

#7


0  

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

这是你想要的术语。如果你需要的话,你可以得到这样的id:

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 );
        }
    }