How to get the taxonomy values of a custom post type

前端 未结 4 1886
清歌不尽
清歌不尽 2021-02-05 22:44

I am creating a new template that will get all the custom post type (Case Studies) content, including the taxonomies values associated with it.

So far I got the follow

相关标签:
4条回答
  • 2021-02-05 23:16

    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;
        } 
    }
    
    0 讨论(0)
  • 2021-02-05 23:32

    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

    0 讨论(0)
  • 2021-02-05 23:33

    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
    
    0 讨论(0)
  • 2021-02-05 23:34

    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
    
    0 讨论(0)
提交回复
热议问题