Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

David Klotz
David Klotz
19,659 Points

how to display custom category on a custom post type

I have created a custom post type "ads" and I want to have custom categories for those ads the same way that wordpress posts have categories by default. I found this bit of code to create custom taxonomies, which displays in the admin properly but I am struggling to figure out how to then get the categories to display on the custom post.

add_action( 'init', 'build_taxonomies', 0 );

function build_taxonomies() { register_taxonomy( 'categories', 'ads', array( 'hierarchical' => true, 'label' => 'Ad Categories', 'query_var' => true, 'rewrite' => true ) ); }

Anyone have any experience with this?

1 Answer

You can do in this way

   $terms = get_the_terms( get_the_ID(), 'your_category' );                                                     
if ( $terms && ! is_wp_error( $terms ) ) : 
   $ads_links = array();

foreach ( $terms as $term ) {
        $ads_links [] = $term->name;
    }                                                   
    $ads_categories = join( ", ", $ads_links ); 
   echo $ads_categories ;  

Hope it will helpful...

David Klotz
David Klotz
19,659 Points

Thanks Khubbaib, I'll give that a try!