How to get the taxonomy values of a custom post type
Asked Answered
O

4

14

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 following:

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

How do I get the taxonomy of it? I have tried multiple things but all seemed to fail and just getting more confused.

Outlawry answered 21/3, 2014 at 16:35 Comment(1)
My final codes looks like this:<?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> <?php $terms = wp_get_post_terms( $query->post->ID, array( 'sectors', 'region', 'equipment' ) ); ?> <?php foreach ( $terms as $term ) : ?> <p><?php echo $term->taxonomy; ?>: <?php echo $term->name; ?></p> <?php endforeach; ?> <?php endwhile; ?>Outlawry
C
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
Cube answered 21/3, 2014 at 19:48 Comment(0)
O
9

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;
    } 
}
Overlong answered 3/10, 2016 at 12:5 Comment(0)
M
1

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

Moreira answered 21/3, 2014 at 17:7 Comment(0)
S
1

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
Spermatozoon answered 2/3, 2018 at 19:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.