how to get the number of total posts in a wordpress category?

I want to show the number of posts in a category and replace that with "99" in my code. this is my codes :

<?php wp_nav_menu( array( 'theme_location' => 'filmview-menu', 'walker' => new wp_materialize_navwalker() ) ); ?>

$('ul.menu-odd').each(function () {
    if ($(this).children().length > 4){
      $(this).addClass("two_cl");
	  $(this).find('li > a').append('<span class="badge menu_num" data-badge-caption="(99)"></span>');
	}
  	});

Upvotes: 0

Views: 7517

Answers (2)

Dipak Kumar Pusti
Dipak Kumar Pusti

Reputation: 1723

You can achieve this by simple WP_Query. And using tax_query for filtering based on custom taxonomy.

$args = array(
    'post_type' => 'YOUR_POST_TYPE',
    'post_status' => 'publish',
    'tax_query' => array(
        array(
            'taxonomy' => 'YOUR_CUSTOM_TAXONOMY',
            'field'    => 'slug', // Can also put 'term_id'
            'terms'    => 'bob',  // Custom taxonomy ID
        ),
    ),
);
$query = new WP_Query( $args );

And you get the total count by post_count variable,

<?php echo $query-> post_count; ?>

For more details on tax_query please check this out,

https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

Hope this one helps.

Upvotes: 0

jeh
jeh

Reputation: 577

$category = get_category($id);
$count = $category->category_count;
echo $count;

get the category by ID and save it in a variable. This variable is now that category object and it has a lot of associated information stored in it. Within the object array there will be the post count. It is called by using $category->category_count and saved as a variable that you can then echo anywhere after that.

Upvotes: 2

Related Questions