Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
How to count the number of sub-terms under each parent term in WordPress

I want to count the number of sub-terms under each parent term in WordPress.

I have created a custom taxonomy in WordPress. I would like to display all the terms of this custom taxonomy on a custom page, such as:

1. I want to display all sub-terms under each parent term in the loop.
2. I want to count the number of sub-terms under each parent term.

The number of posts under each term is being counted. But I'm in trouble with the sub-term.

This is my code.

   <?php 
      $args = array(
          'taxonomy' => 'pharma',
          'get' => 'all',
          'parent' => 0,
          'hide_empty' => 0
      );
      $terms = get_terms( $args );
      foreach ( $terms as $term ) : ?>
      <div class="single_pharma">
        <h2 class="pharma_name"><a href="<?php echo esc_url( get_term_link( $term ) ); ?>"><?php echo $term->name; ?></a></h2>

        <span class="count_category"><span>Generics:</span><?php // want to display here sub term count  ?></span>

        <span class="count_brand"><span>Brands:</span><?php echo $term->count; ?></span>
      </div>
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You could use get_term_childrenDocs function to get all of the "sub_terms":

$args = array(
    'taxonomy'   => 'pharma',
    'get'        => 'all',
    'parent'     => 0,
    'hide_empty' => 0
);

$terms = get_terms($args);

foreach ($terms as $term) {

    $count_sub_terms = count(get_term_children($term->term_id, 'pharma'));

?>
    <div class="single_pharma">
        <h2 class="pharma_name"><a href="<?php echo esc_url(get_term_link($term)); ?>"><?php echo $term->name; ?></a></h2>

        <span class="count_category"><span>Generics:</span><?php echo $count_sub_terms;  ?></span>

        <span class="count_brand"><span>Brands:</span><?php echo $term->count; ?></span>
    </div>
<?php
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda