sub categories display posts loop category.php

Question

I have this kink of hierarchy:

Cat Parent 1
— Child 1
— Child 2

Cat Parent 2
— Cat Child 1
— Post content
— Cat Child 2
— Post content

I wish to display in category.php the subcategory list if there is one or the post if there are no subcategory.
The fist one working well, but for the second I think I have a problem in my query; it’s showing my posts from Child 1 and Child 2

Someone knows what I have to do ?

Thanks

 <?php 

                    $this_category = get_category($cat);

                    if ( get_category_children( $this_category->cat_ID ) != "" ) {

                        $taxonomy = 'category';
                        $terms    = get_terms([
                            'taxonomy'    => $taxonomy,
                            'hide_empty'  => false,
                            'parent'      => get_queried_object_id()
                        ]);
                    
                        foreach ( $terms as $term ) {
                            
                            $term_link = get_term_link( $term, $taxonomy );
                            $image = get_field('image_filaire', 'category_'.$term->term_id); ?>                            

                            <article class="produit encart-categorie">
                            <a href="<?php echo $term_link ?>"><div class="filaire"><?php echo wp_get_attachment_image( $image, 'medium') ?></div>
                                <div class="title <?php echo $catSlug ?>-bk"><span><?php echo ''.$term_id .'' .$term->name.'' ?></span></div></a>                            
                            </article>
                        <?php } 

                    } else {
                        $queried_object = get_queried_object();
                        $term_id = $queried_object->term_id;


                        $the_query = new WP_Query(array(
                            'cat' => $term_id ,
                        ));

                        $post_query = new WP_Query($the_query);

                        if($post_query->have_posts() ) {
                            while($post_query->have_posts() ) {
                                $post_query->the_post(); ?>
                                <article class="produit encart-categorie">
                                <?php echo $term_id ?>
                                <a href="<?php echo the_permalink(); ?>"><div class="filaire"><?php the_post_thumbnail('medium') ?></div>
                                    <div class="title <?php echo $catSlug ?>-bk"><span><h2><?php the_title(); ?></h2></span></div></a>
                                </article>
                                <?php
                                }
                            }

                    } ?>
0
Lust 4 months 0 Answers 22 views 0

Leave an answer