Get categories for a single post in a custom post type

I’m trying to get an unformatted list (preferably a list of slugs) of the categories for a single post in a custom post type loop. This list will eventually serve as a class for a div ($CATEGORYSLUGSWILLEVENTUALLYGOHERE).

I’ve found a few different methods of getting a list of ALL of the categories for a custom post type, but not the ones specific to a single one. Here’s what I have so far:

Read More
<?php $projects_loop = new WP_Query( array( 'post_type' => 'projects', 'orderby' => 'menu_order' ) ); ?>

                        <?php while ( $projects_loop->have_posts() ) : $projects_loop->the_post(); ?>


                            <div class="box <?php $CATEGORYSLUGSWILLEVENTUALLYGOHERE; ?>">

                                    <div class="port-item-home">
                                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'portfolio-home' ); ?></a>
                                        <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
                                    </div>

                                </div>



                    <?php endwhile; ?>

And here’s what I’ve tried so far to get the category list:

<?php
                                    $args = array(
                                      'orderby' => 'name',
                                      'parent' => 0,
                                      'taxonomy' => 'project-type'
                                      );
                                    $categories = get_categories( $args );

                                    echo '<p> '.print_r(array_values($categories)).'something</p>'

                                ?>

I have it returning the array – but the array is showing that it’ll display all categories instead of the ones pertaining to that specific post.

I also tried:

<?php
    //list terms in a given taxonomy (useful as a widget for twentyten)
        $taxonomy = 'project-type';
        $tax_terms = get_terms($taxonomy);
?>

<?php
    foreach ($tax_terms as $tax_term) {
        echo $tax_term->name;
    }
?>

And that also displays all categories instead of the ones pertaining to the post.

Any suggestions??

Related posts

1 comment

  1. Got it! Found this article that helped me out:

    https://wordpress.org/support/topic/how-to-get-the-category-name-for-a-custom-post-type

    <!-- The Query -->
    <?php 
        $args = array( 
            'post_type'      => 'my_post_type', 
            'posts_per_page' => -1, 
            'orderby'        => 'menu_order' );
    
        $custom_query = new WP_Query( $args );
    ?>
    
    <!-- The Loop -->
    <?php 
        while ( $custom_query->have_posts() ) : 
            $custom_query->the_post();
            $terms_slugs_string = '';
            $terms = get_the_terms( $post->ID, 'my_post_type' );
            if ( $terms && ! is_wp_error( $terms ) ) {                
                $term_slugs_array = array();
                foreach ( $terms as $term ) {
                    $term_slugs_array[] = $term->slug;
                }
                $terms_slugs_string = join( " ", $term_slugs_array );
            }
    ?>
    
        <div class="box<?php echo $terms_slugs_string ?>">          
            <div class="port-item-home">
                <a href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail( 'portfolio-home' ); ?>
                </a>
                <a href="<?php the_permalink(); ?>">
                    <?php the_title(); ?>
                </a>
            </div>
        </div>  
    
    <?php endwhile; ?>
    

Comments are closed.