I have a post which is assigned to different categories. I created a loop
<div class="col-sm-12 ">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<section class="row marginbottomclass">
<div class="col-sm-4 thumbnailimage">
<a href="<?php the_permalink(); ?>"> <?php the_post_thumbnail(); ?> </a>
</div>
<div class="col-sm-8">
<h1><?php the_title( ); ?></h1>
<small><?php the_category("," );?></small>
<p><?php the_excerpt(); ?></p>
</div>
</section>
<div class="clearfix"></div>
<?php endwhile; else : ?>
<?php endif; ?>
</div>
But this loop getting all the category of post. While i only want to show one category. I don't want to show all the category that this post assigned to.
I tried different
<?php $parentscategory ="";
foreach((get_the_category()) as $category) {
if ($category->category_parent == 0) {
$parentscategory .= ' <a href="' . get_category_link($category->cat_ID) .
'" title="' . $category->name . '">' . $category->name . '</a>, ';
}
}
echo substr($parentscategory,0,-2); ?>