Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

209
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda