Quantcast
Channel: Extreme Web Designs » wordpress-snippets
Viewing all articles
Browse latest Browse all 10

WordPress: Total post count of a Category and its Child Categories

$
0
0

Robert

It may be desirable to show how may posts a Category contains in it & also display the post count that the child categories of this (parent) category contains. So in WordPress to display total post count of a Category and its Child Categories, use the following little snippet of code.

How to get total post count of a Category and its Child Categories in WordPress

Simply paste the following snippet in functions.php file of your WordPress theme:

<?php

function get_post_count($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;

}

?>

Then simply paste the following code to display the post count of the Category that you desire & it’s child categories. Simply paste the following code where you want the posts count to appear in your WordPress theme:

<?php

// To get post count of category having category id 10.
//Change 10 to a number of your choice
echo get_post_count(10);

?>

Your Turn!

Do you know of any other ways to get total post count of a Category and its Child Categories in WordPress? If yes, please feel free to share by commenting below.

WordPress: Total post count of a Category and its Child Categories


Viewing all articles
Browse latest Browse all 10

Trending Articles