programing

사용자 지정 게시 유형의 분류법 값을 가져오는 방법

goodjava 2023. 3. 21. 22:07

사용자 지정 게시 유형의 분류법 값을 가져오는 방법

관련된 분류 값을 포함하여 모든 커스텀 포스트 유형(케이스 스터디) 콘텐츠를 가져올 새 템플릿을 만듭니다.

지금까지 다음과 같은 정보를 얻었습니다.

<section>
<h1><?php _e( 'posts', 'casestudies' ); ?></h1>
<?php get_template_part('loop'); ?>
<?php
$args = array('post_type' => 'casestudies', 'posts_per_page' => 3);
$query = new WP_Query($args);
while($query -> have_posts()) : $query -> the_post();
?>
<h2><?php the_title(); ?></h2>
<p>Meta: <?php the_meta(); ?></p>
<p>Excerpt: <?php the_excerpt(); ?></p>
<p>what_to_put_here_to_get_taxonomies_values????</p>
<?php endwhile; ?>

<?php get_template_part('pagination'); ?>
</section>

분류법은 어떻게 알 수 있나요?여러 가지 시도를 해봤지만 모두 실패한 것 같고 더 혼란스러울 뿐입니다.

다음 함수를 체크합니다.wp_get_post_terms()

커스텀 포스트 타입의 케이스 스터디에서 국가서브젝트라고 불리는2개의 분류법이 서포트되고 있는 경우, 다음과 같이 시험해 볼 수 있습니다.

<?php $terms = wp_get_post_terms( $query->post->ID, array( 'country', 'subject' ) ); ?>
<?php foreach ( $terms as $term ) : ?>
<p><?php echo $term->taxonomy; ?>: <?php echo $term->name; ?></p>
<?php endforeach; ?>

출력은 다음과 같습니다.

Country: United Kingdom
Subject: Biology
Subject: Chemistry
Subject: Neurology

가정: 사용자 지정 게시 유형 이름 publication_category에 분류법을 등록했습니다.

커스텀 투고 타입의 템플릿에 다음과 같이 입력합니다.

$terms = get_the_terms( $post->ID, 'publication_category' );
if ($terms) {
    foreach($terms as $term) {
      echo $term->name;
    } 
}

사용해보셨습니까?<?php get_taxonomies() ?>?

기능하는 특정 분류법에 옵션인수가 있는 경우 전달하여 출력을 제어할 수 있습니다.다음 문서를 참조하십시오.http://codex.wordpress.org/Function_Reference/get_taxonomies

혹시 도움이 될까봐 커스텀 투고 타입의 루프 안에서 'the_taxonomies()' 함수를 사용했습니다.

        <?php

        while ( have_posts() ) : the_post();    
          $custom_post = get_post_meta( get_the_ID() );       
          //
        ?>

        //html
        //and stuff

        <?php the_taxonomies(); ?>

        <?php
          endwhile;
        ?>


 the result was:

   Taxonomy-name: {Taxonomy-term}. <-- as a link

언급URL : https://stackoverflow.com/questions/22564382/how-to-get-the-taxonomy-values-of-a-custom-post-type