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


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

            // 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() ) {
                    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 class="col-7">
                            <a href="<?= get_permalink(); ?>"><?= the_title()?></a>
                            $category_array = get_the_terms(get_the_ID(), 'product_cat');
                        <div class="col-3 text-right ">Price</div>
            } else {
                // no posts found

            // Restore original Post Data
davidb3rn 4 months 0 Answers 11 views 0

Leave an answer