WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] display the posts count of the selected category (12 posts)

  1. Joun007
    Member
    Posted 3 years ago #

    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?

  2. Chip Bennett
    Theme Review Admin
    Posted 3 years ago #

    Try something like this:

    $customcats = get_categories();
    $categorycount = $customcat->count;
  3. Joun007
    Member
    Posted 3 years ago #

    thanks Chip, but didn't work it shows nothing :)

  4. Chip Bennett
    Theme Review Admin
    Posted 3 years ago #

    Did you try:
    echo $categorycount;

  5. Joun007
    Member
    Posted 3 years ago #

    <?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

  6. Chip Bennett
    Theme Review Admin
    Posted 3 years ago #

    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.)

  7. Joun007
    Member
    Posted 3 years ago #

    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, :)

  8. 123milliseconds
    Member
    Posted 3 years ago #

    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.

  9. Joun007
    Member
    Posted 3 years ago #

    its not solved yet :S

  10. Chip Bennett
    Theme Review Admin
    Posted 3 years ago #

    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.

  11. Joun007
    Member
    Posted 3 years ago #

    thanks Chip, it worked now :D

  12. Chip Bennett
    Theme Review Admin
    Posted 3 years ago #

    Excellent!

Topic Closed

This topic has been closed to new replies.

About this Topic