Quantcast
Channel: Wordpress – Nilesh Shiragave
Viewing all articles
Browse latest Browse all 19

total post count of a category and its child categories wordpress

$
0
0

Use following code to get total posts count of a category and all its child categories. To use this code you have to add following function inside your themes functions.php file

function wp_get_postcount($id)
{
//return count of post in category child of ID 15
$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 yo use this code to display post count call this function anywhere inside your wordpress template files as

<?php
echo wp_get_postcount(5);
// To get post count of category having category id 5
?>

This code snippet is useful to get total post count from category and its child categories.
Change 5 with your category id for which you want total post count.

Reference


Viewing all articles
Browse latest Browse all 19

Trending Articles