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
どうでも!見つけた :)
get_queried_object()->term_id;
必要なコードスニペット全体は次のとおりです。
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
<?php
$terms = get_the_terms( $post->ID, 'taxonomy');
foreach ( $terms as $term ) {
$termID[] = $term->term_id;
}
echo $termID[0];
?>
スラッグという用語が必要です。必要な場合は、次のように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 );
}
}