Forums

Post count by Parent Category (2 posts)

  1. heinzawhtet
    Member
    Posted 7 months ago #

    I want to show post count in my site by parent category.

    Example -
    We have 18 Tutorials, 15 Article, 3 Freebies

    Help!

  2. NateJacobs
    Member
    Posted 7 months ago #

    If your categories are already pluralized you can use this:

    $categories = get_categories();
    echo "We have ";
    foreach( $categories as $category )
    {
    	echo $category->count.' '.$category->name.' ';
    }

    If you would like to pluralize automatically you need two functions:

    function ngtj_pluralize($count, $singular, $plural = false)
    {
    	if (!$plural) $plural = $singular . 's';
    	return ($count == 1 ? $singular : $plural) ;
    }

    and

    function ngtj_list_cats_plural()
    {
    	$categories = get_categories();
    
    	echo "We have ";
    
    	foreach( $categories as $category )
    	{
    		$cat_name = ngtj_pluralize( $category->count, $category->name, $category->name.'s' );
    		echo $category->count.' '.$cat_name.' ';
    	}
    }

Reply

You must log in to post.

About this Topic

Tags