Support » Themes and Templates » display the posts count of the selected category

  • Resolved Mahmud Hamid

    (@joun007)


    hi, i’m using a post query before the loop in my index file:

    <?php
    $args = array(
    	'category_name' => 'myworks',
    	'showposts'  => '3',
    	);
    $the_query = new WP_Query( $args );
    ?>
    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>

    now what i want is to display the total number of posts in the category mentioned in $args but before the loop, like this for example:

    <?php
    $args = array(
    	'category_name' => 'myworks',
    	'showposts'  => '3',
    	);
    $the_query = new WP_Query( $args );
    ?>
    
    <div id="info"><div class="count"><p>total posts:<?php get the total posts from the category above ?></p></div></div>
    
    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>

    any solution?

Viewing 11 replies - 1 through 11 (of 11 total)
  • Chip Bennett

    (@chipbennett)

    Theme Review Admin

    Try something like this:

    $customcats = get_categories();
    $categorycount = $customcat->count;

    thanks Chip, but didn’t work it shows nothing ๐Ÿ™‚

    Chip Bennett

    (@chipbennett)

    Theme Review Admin

    Did you try:
    echo $categorycount;

    <?php
    $args = array(
    	'category_name' => 'myworks',
    	'showposts'  => '3',
    	);
    $the_query = new WP_Query( $args );
    ?>
    <div id="featured-info"> <div id="featured-name" ><p><?php echo $args['category_name'] ;?></p></div><div id="featured-count"><p>total postsย <?php $customcats = get_categories(); $categorycount = $customcat->count; echo $categorycount; ?></p></div></div>
    
    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>

    this is the code i used

    Chip Bennett

    (@chipbennett)

    Theme Review Admin

    Try using get_the_category() instead of get_categories()?

    (Note: I wasn’t giving you exact, working code, but rather an example usage of the function that returns the object that contains the category count. Refer to the linked Codex docs for information regarding how to implement for your specific use case.)

    thanks Chip, i was using this code:

    <?php $cat = get_the_category(); $cat = $cat[0];?>
    <?php echo $cat->cat_name;?>
    <?php echo $cat->category_count; ?>

    and it was running fine but when i tried to add another query for another category in a second loop in my index, it wont work it would show me the name of the new category but the count of the previous query category.

    so i was thinking maybe my method is wrong in such case and should use another one.

    hope i could solve this matter.
    thanks for your kind help, ๐Ÿ™‚

    Please mark thread as resolved so that

    – others with similar problem can see it as resolved and will read this thread for help if they have similar problem

    – people providing help see it as resolved and will not waste time reading this post.

    its not solved yet :S

    Chip Bennett

    (@chipbennett)

    Theme Review Admin

    If you’re running two instances of WP_Query on the same page (via your custom loop, and the main call to query_posts(), then you might need to do a wp_reset_query() between them, in order to reset the query so that you get the correct post count.

    thanks Chip, it worked now ๐Ÿ˜€

    Chip Bennett

    (@chipbennett)

    Theme Review Admin

    Excellent!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘display the posts count of the selected category’ is closed to new replies.