• Resolved ekdor

    (@ekdor)


    I can’t get the total post count for the number of posts in a category that is selected. I understand that is a bit confusing. If I click on a category or taxonomy in my menubar I get a list of posts belonging to that category or taxonomy. All are headed with the name of the category or taxonomy. I have it showing a set number with the navigation pref/next links.

    What I’m trying to get working is adding the count of posts belonging to that category or taxonomy next to the heading. Preferably I would like it to show something like showing 50 of 150 etc.

    Anyway, all my attempts either breaks the site, lists all categories with their total counts or nothing shows up at all.

    Its seems wp_count_posts() is what I need, except I can’t get it to work. Bellow is what I’m currently using for the heading in my categories template:

    <H3>Showing all items for:</H3>
    <H2><?php single_cat_title() ?>

    The follow is one of my attempts that I think is on the right track but returns no result:

    <H3>Showing all items for:</H3>
    <H2><?php single_cat_title() ?> - <?php $count_posts = wp_count_posts(); ?>

    Also if someone has the answer, can you also get this to work on a taxonomy category?

    Thanks and Cheers,.

Viewing 10 replies - 1 through 10 (of 10 total)
  • You can use get_term() like this:

    $term_id  = 1; // ID of taxonomy term
    $taxonomy = 'category';
    $term_obj = get_term( $term_id, $taxonomy );
    echo '<p>There are ' . $term_obj->count . ' posts in the ' . $term_obj->name . ' category.</p>';

    Change the value $term_id to the ID number of your term, and change the value of $taxonomy to the slug of your custom taxonomy if you are using one.

    Thread Starter ekdor

    (@ekdor)

    Ok Thanks,

    Well I got a result this time. But it returns the value zero for categories.

    Cheers,.

    Thread Starter ekdor

    (@ekdor)

    Also, for the taxonomy. I use a single taxonomy template but have 7 separate taxonomies. 5 have menubar links but all are listable. How would I get it to distinguish the appropriate taxonomy filter being displayed?

    Cheers,.

    Thread Starter ekdor

    (@ekdor)

    Hmm I see. I replaced the $term_id with the appropriate number from one of my categories. This worked. However because this is entered in the category template it means all categories will show the number for that one category. I need it to acquire the $term_idfor the category/taxonomy that has been selected.

    Cheers,.

    Thread Starter ekdor

    (@ekdor)

    WooHoo. I got what I wanted. I found a bit of code that determine the catagory ID and placed it into the $term_id. It’s as follow:

    $term_id  = $id = get_queried_object_id(); // ID of taxonomy term
    $taxonomy = 'category';
    $term_obj = get_term( $term_id, $taxonomy );
    echo '<p>There are ' . $term_obj->count . ' posts in the ' . $term_obj->name . ' category.</p>';

    Cool…

    Thanks for your help.

    Thread Starter ekdor

    (@ekdor)

    However I can’t get it to work on my Search and Taxonomy templates.

    Thread Starter ekdor

    (@ekdor)

    $term_id  = $id = get_queried_object(); // ID of taxonomy term
    $taxonomy = 'category';
    $term_obj = get_term( $term_id, $taxonomy );
    echo '<p>There are ' . $term_obj->count . ' posts in the ' . $term_obj->name . ' category.</p>';

    This seems to work better. Works for taxonomy also, I just had to put the slug for one of my taxonomies in place of 'category'. Doesn’t seem to effect the results of my count; which is the only part of it that I wanted. My code as follows:

    <DIV CLASS="listHeading">
    <H3>Showing <SPAN CLASS="entryNo"><?php $term_id  = $id = get_queried_object();
    $taxonomy = 'source';
    $term_obj = get_term( $term_id, $taxonomy );
    echo $term_obj->count; ?></SPAN> items for:</H3>
    <H2><?php single_cat_title() ?></H2>
    </DIV>

    Still not working for the search results template… Tried using the values given in my search field tag.

    Cheers,.

    Yeah, get_queried_object() will have different results on a taxonomy archive than it will on a search archive.

    For search, you want to use get_search_query(). However, that will get you the search query, not a taxonomy term…so in that case you would want to count search results instead, yeah?

    To echo the number of search results found, use this:

    <?php echo $wp_query->found_posts; ?>

    In fact, you might be able to just use that on your taxonomy archives as well! ??

    Yeah, use something like this on your taxonomy archive and search archive:

    <?php
        $page_number = is_paged() ? $wp_query->query_vars['paged'] : 1;
        $current_max = $wp_query->post_count * $page_number;
        $current_min = $current_max - $wp_query->post_count + 1;
    ?>
    Showing <?php echo $current_min; ?> - <?php echo $current_max; ?> of <?php echo $wp_query->found_posts; ?> posts.
    Thread Starter ekdor

    (@ekdor)

    The <?php echo $wp_query->found_posts; ?> worked a treat! Thanks

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Can't get a total count for posts in listed loop of category’ is closed to new replies.