Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the taxonomy values of a custom post type

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.

like image 898
user2091936 Avatar asked Mar 21 '14 16:03

user2091936


People also ask

How do I find the taxonomy of a custom post type in WordPress?

In WordPress, you can create (or “register”) a new taxonomy by using the register_taxonomy() function. Each taxonomy option is documented in detail in the WordPress Codex. After adding this to your theme's functions. php file, you should see a new taxonomy under the “Posts” menu in the admin sidebar.

What is taxonomy in custom post type?

A WordPress taxonomy is a way to organize groups of posts and custom post types. The word taxonomy comes from the biological classification method called Linnaean taxonomy. By default, WordPress comes with two taxonomies called categories and tags. You can use them to organize your blog posts.


2 Answers

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;
    } 
}
like image 190
Nurealam Sabbir Avatar answered Oct 03 '22 14:10

Nurealam Sabbir


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
like image 39
Ed Vieira Avatar answered Oct 03 '22 16:10

Ed Vieira