0

I am creating a page template in Wordpress that displays multiple tags that are in a particular category. I have this working, but now I want to have the number of posts within each tag that is displayed as well like if I had a tag called apples with 5 posts it would look like this:

Apples(5)

As of now it just shows Apples

Here is my code I want to modify:


    <?php
      if (is_category()){
           $cat = get_query_var('cat');
           $yourcat = get_category ($cat);
       }
       $tag_IDs = array();
           query_posts('category_name=health');
                   if (have_posts()) : while (have_posts()) : the_post();
                       $posttags = get_the_tags();
                           if ($posttags):
                           foreach($posttags as $tag) {
                               if (!in_array($tag->term_id , $tag_IDs)):
                                   $tag_IDs[] = $tag->term_id;
                                   $tag_names[$tag->term_id] = $tag->name;
                                   endif;
                       }
                       endif;
                       endwhile; endif;
                   wp_reset_query();
       
               echo "<ul>";
               foreach($tag_IDs as $tag_ID){
               echo '<a href="'.get_tag_link($tag_ID).'">'.$tag_names[$tag_ID].'</a>';
               }
           echo "</ul>";
       ?>                                  
  • 1
    I see you've re-posted this question several times and then deleted it and reposted it. You shouldn't do that, you'll get flagged by the automated system for suspicious behaviour. It'll also put off people posting answers. Stop it. If nobody answers your question, edit it to make it more attractive, don't spam the site – Tom J Nowell Sep 13 at 21:30
1
function wp_get_postcount($id)
{
    $contador = 0;
    $taxonomia = 'categoria';
    $args = array(
        'child_of' => $id,
    );
    $tax_terms = get_terms($taxonomia,$args);
    foreach ($tax_terms as $tax_term){
        $contador +=$tax_term->contador;
    }
    return $contador;
}
| improve this answer | |
  • Can you edit your answer to explain how this answers the OP's question? – Tom J Nowell Sep 13 at 21:32
0

The reason you get this is because you're using query_posts and looping over posts, not terms/categories.

So while you want this:

terms = child terms of health
for each term in terms
    print the term name
    print the number of posts in that term

What the code actually does is this:

posts = all posts that have the category health
for each posts
    print a list of tags this post has

query_posts, aside from being a function you should never use, fetches posts, not terms/categories. It does not make sense to fetch posts here.

So instead we need to do this:

healthterm = the health term
terms = child terms of health term
for each term in terms
    print the name
    print the post count

So first we get the health term:

$parent = get_term_by('name', 'health', 'category')

Then get its children with something similar to this:

$terms = get_terms( 'category', [
    'parent' => $parent->term_id
] );

And finally, loop over the terms and print their names/counts

if ( !is_wp_error( $terms ) && !empty( $terms ) ) {
    foreach ( $terms as $term ) {
        // ...
        echo $term->name . ' ' .$term->count;
    }
}

These are the missing building blocks you need to achieve your goal.

| improve this answer | |
  • This did not work. It just spit out how many posts I have in each category. – RLM Oct 13 at 22:11
  • But that's the title of your question, this answer gets the health tag and prints out the post count of its child terms. If that's not what you wanted then you need to update your question to ask what you wanted. Unless you're looking for a copy paste solution that requires no understanding, in which case I cannot provide that. – Tom J Nowell Oct 14 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.