admin管理员组

文章数量:1316617

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

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

Share Improve this question edited Nov 2, 2020 at 22:38 Tom J Nowell 61k7 gold badges79 silver badges148 bronze badges asked Sep 13, 2020 at 20:51 RLMRLM 2475 silver badges19 bronze badges 1
  • 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 Commented Sep 13, 2020 at 21:30
Add a comment  | 

3 Answers 3

Reset to default 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;
}

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( 'slug', '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.

Here is the code I rewrote that solved my problem. It loops through a category and shows how many articles all the tags inside the category has.

<?php
                            $custom_query = new WP_Query( 'posts_per_page=-1&category_name=health' );
                            if ( $custom_query->have_posts() ) :
                                $all_tags = array();
                                while ( $custom_query->have_posts() ) : $custom_query->the_post();
                                    $posttags = get_the_tags();
                                    if ( $posttags ) {
                                        foreach($posttags as $tag) {
                                          $all_tags[$tag->term_id]['id'] = $tag->term_id;
                                          $all_tags[$tag->term_id]['name'] = $tag->name;
                                          $all_tags[$tag->term_id]['count']++;
                                        }
                                    }
                                endwhile;
                            endif;
    
                            $tags_arr = array_unique( $all_tags );
                            $tags_str = implode( ",", $tags_arr );
    
                            $args = array(
                                'smallest'  => 12,
                                'largest'   => 12,
                                'unit'      => 'px',
                                'number'    => 0,
                                'format'    => 'list',
                                'include'   => $tags_str
                            );
                            foreach ( $all_tags as $tag_ID => $tag ) {
                                echo '<a href="'.get_tag_link($tag_ID).'">' . $tag['name'] . '</a> ('; echo $tag['count'] . ')<br>';
                            }
                            wp_reset_query()
                         ?>

本文标签: categoriesShow the amount of posts in a tag in a specific category has