How do I make my terms for each product display via foreach loop? (woocommerce)

Question

This is my current loop to display my products via woocommerce. When I do print_r($category_array); It returns the array, but when I try to use a function to call it so I can do what I want to with the data, it makes and my entire screen doesn’t display after the loop. Maybe it’s a mistake in my function? Still very new to woocommerce and wp_loops. Thank you

            <?php
            // WP_Query arguments
            $args = array(
                'p'                      => 'product',
                'post_type'              => array( 'product' ),
                'order'                  => 'ASC',
                'post_per_page' => 20,
            );

            // The Query
            $query = new WP_Query( $args );

            // The Loop
            if ( $query->have_posts() ) {
                while ( $query->have_posts() ) {
                    $query->the_post();
                    function filter_categories($categories) {
                            foreach ($categories as $category) {
                                echo $category->name;
                            }
                    }


                    ?>
                    <div class="row">
                        <div class="col-2">
                            <?php echo the_post_thumbnail(get_the_ID(), 'thumbnail'); ?>
                        </div>
                        <div class="col-7">
                            <a href="<?= get_permalink(); ?>"><?= the_title()?></a>
                            <br/>
                            <?php
                            $category_array = get_the_terms(get_the_ID(), 'product_cat');
                            filter_categories($category_array);
                            ?>
                        </div>
                        <div class="col-3 text-right ">Price</div>
                    </div>
                    <?php
                }
            } else {
                // no posts found
            }

            // Restore original Post Data
            wp_reset_postdata();
            ?>
0
davidb3rn 4 months 0 Answers 11 views 0

Leave an answer