Display category name only once inside loop

I have been researching on how I can output the category name on a wordpress loop for custom post only to display only once. If there are any posts under the same category, the category name will not be display only the first one. I tried searching the codex for “the_category” but there wasn’t anything there that could help me with my problem. Here is the sample of the code I have been working.

<?php

$film_timeline = new WP_Query( array('post_type' => 'film_timeline') );

if ( $film_timeline->have_posts() ) : while ( $film_timeline->have_posts() ) : $film_timeline->the_post();


?>


<h3 class="category-name"><?php the_category(); ?></h3>


<?php wp_reset_postdata(); ?>

<?php endwhile; else: ?>

<?php //Insert Error Message ?>

<?php endif; ?>

<?php wp_reset_query(); ?>

Part of the problem is that you have your category name being displayed inside the loop, so of course it will display each time a post is displayed.

What exactly are you trying to accomplish? Are you trying to build an archive based on the categories of this custom post type?

I’m trying to build a list of post based under different categories. I want the category name to appear only once.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.