Display the first child category assigned to a post with a + sign after and total count

Description

This code will Display the first child category assigned to a post with a + sign after and total count for Child category 267. Simply replace 267 with the id of needed category.

PHP Source Code

                        
                          <!--Get category + More Count Start--> 
<?php
	// Display the first category under ancestor 267 and count additional categories
	$categories = get_the_category();
	$ancestor_id = 267; // The ancestor category ID
	$first_category = null;
	$additional_count = 0;

	foreach ($categories as $category) {
		if (cat_is_ancestor_of($ancestor_id, $category)) {
			if (!$first_category) {
				$first_category = $category;
			} else {
				$additional_count++;
			}
		}
	}

	if ($first_category) {
		echo $first_category->cat_name;
		if ($additional_count > 0) {
			echo ' + ' . $additional_count . ' more';
		}
		
	}
?>
<!--Get category + More Count End-->                        
                        


Post Categories