Get current taxonomy post count

Is there a way to get the current taxonomy (category or tag) post count? The code i’m having is only for category

<?php 
    $cat = get_query_var( 'cat' );
    $categories = get_categories( 'include='.$cat );
    if ( $categories ) { 
        foreach( $categories as $category ) { 
            echo '' . $category->count;
        }
    }
?>

Related posts

Leave a Reply

1 comment

  1. I guess you can do something like this:

    // Set the name of your Taxonomy or get it as you're currently doing
    // It can be category, tag or custom taxonomy name
    $taxonomy = "your_taxonomy"; 
    
    $total_count = 0;
    
    // Get all the terms in your Taxonomy and add the count for each term
    foreach ( get_terms( $taxonomy ) as $term ) {
        $total_count += (int) $term->count;
    }
    
    echo $total_count;
    

    This will give you the count of all the posts that have assigned ANY term in the Taxonomy your_taxonomy, which I understand is what you want…