programing

WordPress에서 (slug가 아닌) 현재 분류 용어 ID를 얻는 방법은 무엇입니까?

sourcejob 2022. 12. 3. 00:32
반응형

WordPress에서 (slug가 아닌) 현재 분류 용어 ID를 얻는 방법은 무엇입니까?

분류법을 만들었죠php 페이지는 WordPress 테마 폴더에 있습니다.함수의 현재 용어 ID를 받고 싶습니다.이거 어떻게 구하지?

get_query_var('taxonomy')slug라는 용어만 반환한다. 아이디를 원한다.

됐어!찾았어 :)

get_queried_object()->term_id;

심플하고 간단해!

get_queried_object_id()

필요한 코드 스니펫은 다음과 같습니다.

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

다음 코드 사용

현재 분류법 이름과 설명이 인쇄됩니다(옵션).

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

분류법 페이지에 있는 경우.

그래야 분류법에 대한 모든 세부 정보를 얻을 수 있습니다.

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

분류법 ID를 가져오는 방법

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

단, 포스트 페이지(택시 -> 아이)에 있는 경우

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

wp_get_post_param()를 참조해 주십시오.

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

print_r($terms);

네가 원하는 건 민달팽이야필요한 경우 다음과 같은 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 );
        }
    }

이는 분류법 용어 아카이브뿐만 아니라 모든 페이지 유형에서 작동합니다.

$category_id = get_the_category( get_the_ID());
$id          = get_category($category_id[0]->term_id);

언급URL : https://stackoverflow.com/questions/12289169/how-to-get-the-current-taxonomy-term-id-not-the-slug-in-wordpress

반응형