Total Post Count of a Category in WordPress

Some times you need to display total posts count in each category, on such situation you can use the following set of code into your Word Press theme. For displaying total post count of category you must add the below code into your functions.php.

function wp_get_postcount($id)
{
$count = 0;
$taxonomy = 'category';
$args = array(
  'child_of' => $id,
);
$tax_terms = get_terms($taxonomy,$args);
foreach ($tax_terms as $tax_term) {
$count +=$tax_term->count;
}
return $count;
}

And you can call above function where ever in your template file as follows.

<?php
echo wp_get_postcount(3);
// This will echo total count of postesunder ID 3
?>

Related posts:

Partner with Us

We are providing an exclusive partnership offer to individuals and companies who are interested in setting up their own web design & development company. For more information about how to partner with us, please contact us