Display custom categories in custom post loop

I am learning to create a Portfolio section. I already created the custom post type and a custom taxonomy for the portfolio categories. The categories are working ok, i can choose which category i want for every portfolio item.

I am trying to loop within the post_type portfolio to get the items and it works ok, but i cannot get the categories of each item.

Read More
<?php $loop = new WP_Query( array( 'post_type' => 'portfolio') ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

<div class="panel">
    <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
    <p><?php the_content(); ?></p>
    <p><?php the_category(); ?></p>
</div>

<?php endwhile; wp_reset_query(); ?>

I am using the above code, but the categories dont show up.

I tried separately to display the categories and are working ok with this code:

<?php
    $args = array( 'taxonomy' => 'portfolio_categories', );
    $categories = get_categories($args);
        foreach($categories as $category) { ?>
            <?php echo $category->name;?>
    <?php } 
?>

So how can i display each portfolio item categories in the loop ?

Related posts

Leave a Reply

1 comment

  1. Try following code, this will print all custom taxonomies of post.

    <?php 
    $terms = get_the_terms( $post->ID, 'portfolio_categories' );
    if ( $terms && ! is_wp_error( $terms ) ) :
    
        $taxonomies = array();
        foreach ( $terms as $term ) {
            $taxonomies[] = $term->name;
        }
    
        $taxonomies = implode(", ", $taxonomies );
        ?>
    
        <p class="Custom-Taxonomies">
            Custom Taxonomies: <span><?php echo $taxonomies; ?> </span>
        </p>
    
    <?php endif; ?>