taxonomy – get_the_category – display only a single category for a post (not all categories)

Question

I am trying to display only one post category for a post, instead of them all. My current code is:

<?php
    $category = get_the_category($post_id);

    foreach($category as $cat){
        if ( $cat->cat_ID == '3'){
        
?>
<a href="<?php echo get_category_link($cat->cat_ID); ?>"><li><?php echo $cat->name ?></li></a>
<?php
}
}
?>

I know the following code is incorrect, but not sure how to to go about calling only 1 category.

if ( $cat->cat_ID == '3'){

Any help would be greatly appreciated!

Thanks

0
user1004523 10 months 2022-08-08T00:18:02-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse