How to get the taxonomy values of a custom post type

柔情痞子 提交于 2019-12-03 03:02:16

Check this function: wp_get_post_terms()

Assuming your custom post type Case Study supports two taxonomies called country and subject, you can try something like this:

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

Your output would be something like:

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

assume: I register a taxonomy with custom post type name publication_category.

On your custom post type template write :

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

Have you tried using <?php get_taxonomies() ?> ?

If your looking for specific taxonomies that function has optional arguments you can pass in to control the output. See documentation here : http://codex.wordpress.org/Function_Reference/get_taxonomies

Just in case if it could help someone, I used "the_taxonomies()" function inside a loop of a custom post type.

        <?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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!