Shahulktm
Shahulktm

Reputation: 299

Display all categories assigned to a custom post type using post id wordpress

i have created a custom post type in my WordPress site. and i am added many posts to that custom post type.

i want to display all categories assigned to each posts in that custom post type. please help me. thankz...

Upvotes: 0

Views: 12695

Answers (4)

csehasib
csehasib

Reputation: 365

<?php
$custom_post_taxonomies = get_object_taxonomies('your_taxonomy_name');

if(count($custom_post_taxonomies) > 0)
{
 foreach($customPostTaxonomies as $tax)
 {
     $args = array(
          'orderby' => 'name',
          'show_count' => 0,
          'pad_counts' => 0,
          'hierarchical' => 0,
          'taxonomy' => $tax,
          'title_li' => ''
        );

     wp_list_categories( $args );
 }
}
?>

Please replace "your_taxonomy_name" by your taxonomy name. It's working nicely.

Upvotes: 1

AJ1
AJ1

Reputation: 82

Use Wordpress functions:

has_term: http://codex.wordpress.org/Function_Reference/has_term

and get_the_terms: http://codex.wordpress.org/Function_Reference/get_the_terms

It is important to use has_term(), so you don't get errors on posts that do not use custom taxonomies.

get_the_terms() gets all the terms for the current post ID, use this inside the loop.


example:

<ul class="post-entry-meta">

<?php if( has_term('','your taxonomy' ))

{

$terms = get_the_terms( $post->ID , 'your taxonomy' ); 

foreach ( $terms as $term )

{echo '<li class="taxonomy-list">', $term->name , '</li>' ;}

}?> 

</ul>

Upvotes: 1

Mike
Mike

Reputation: 2735

You can try:

$terms = get_the_terms($post->ID, 'your_taxonomy_name');

http://codex.wordpress.org/Function_Reference/get_the_terms

but you have to set a taxonomy for you CPT first:

http://codex.wordpress.org/Function_Reference/register_taxonomy

Upvotes: 2

Bhumi Shah
Bhumi Shah

Reputation: 9476

you can try below code

foreach ( get_posts( 'numberposts=-1') as $post ) {

            wp_set_object_terms($post_id, 'category_name', 'category');
    }

Upvotes: 1

Related Questions