V
V
Vadim Kinev2021-05-12 14:51:34
Taxonomy
Vadim Kinev, 2021-05-12 14:51:34

How to display subcategories of a cast type category on the archive page?

There is a catalog page, it displays all the categories of this taxonomy
. Also, the entries that are in each of the categories are displayed directly to each category card.
To this list, you need to display a link to the child category of each category.
How can this be done?

<?php
$terms = get_terms(
  array(
    'taxonomy'   => 'catalogCategories',
    'orderby'       => 'id',
  'order'         => 'ASC',


  )
);



  
if ( ! empty( $terms ) && is_array( $terms ) ) {
  foreach ( $terms as $term ) { 

    ?>

  <div class="catalog-card">
    <a href="<?php echo esc_url( get_term_link( $term ) ) ?>">
      <div class="front-card">
        <div class="image-container">
          <?php if($imgcat1=get_field("category_img",get_category($term))){?>
            <img src="<?php echo $imgcat1; ?>">
            <?php }?>
        </div>
        <h5><?php echo $term->name; ?></h5>
      </div>
      <div class="back-card">
        <div class="text-container">
          <h5><?php echo $term->name; ?> </h5>
          <ul>

            <?php
                $mypost = array(
                  'posts_per_page' => 9,
                  'tax_query' => array(
                    array(
                      'taxonomy' => 'catalogCategories',
                      'field' => 'slug',
                      'terms' => $term->slug
                    ),
                  ),
                );
                $loop = new WP_Query( $mypost );
              ?>
              <?php while ( $loop->have_posts() ) : $loop->the_post();?>

                <li>
                  <a href="<?php echo get_permalink(); ?>">
                    <?php echo get_the_title( $ID ); ?>
                  </a>
                </li>

                <?php endwhile; ?>
          </ul>

        </div>
      </div>
    </a>
  </div>

  <?php
  }
}
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Zolin, 2021-05-12
@Heinsed

Use get_term_children()
$terms = get_term_children( $term, $taxonomy );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question