138

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?

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

4

9 に答える 9

337

どうでも!見つけた :)

get_queried_object()->term_id;
于 2012-09-05T20:32:02.767 に答える
44

必要なコードスニペット全体は次のとおりです。

$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
于 2012-09-22T01:15:39.980 に答える
10
<?php 
$terms = get_the_terms( $post->ID, 'taxonomy');
foreach ( $terms as $term ) {
    $termID[] = $term->term_id;
}
echo $termID[0]; 
?>
于 2015-03-15T23:04:47.703 に答える
2

スラッグという用語が必要です。必要な場合は、次のように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 );
        }
    }
于 2017-06-10T14:20:54.893 に答える