具有类别过滤器的Woocommerce产品循环

时间:2018-11-22 10:16:19

标签: php wordpress loops woocommerce

我正在尝试集成woocommerce产品循环以使用类别过滤器。目前,如果使用普通的帖子类别,它可以很好地工作,但是如果不破坏它,我就无法使其与woocommerce产品类别一起使用。我已在代码中注释了使我有问题的部分,如下所示。

<ul class="portfolio-filter">
        <li><a class="active" href="#" data-filter="*">All</a></li>
        <?php
            $args = array(
                'hide_empty'=> 0,
                'orderby' => 'name',
                'order' => 'ASC'
            );

            //I want to list woocommerce categories 
            $categories = get_categories($args);
            foreach($categories as $category) { 
                echo 
                    '<li>
                        <a href="#" data-filter=".'.$category->name.'">    
                            '.$category->name.'
                        </a>
                    </li>';
            }
        ?>

    </ul>

    <div class="row">
        <div class="portfolio-items">

           <?php
                //i want to replace 'cat' => $category_id with recommended woocommerce cat id
                $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 4, 'cat' => $category_id, 'orderby' =>'date','order' => 'DESC' );

                $loop = new WP_Query( $args );
                while ( $loop->have_posts()) : $loop->the_post(); global $product;
                if ( $attachments = get_children(
                    array(
                    'post_type' => 'attachment',
                    'post_mime_type'=>'image',
                    'numberposts' => 99,// -1 to get all images
                    'post_status' => null,
                    'post_parent' => $post->ID
                    )
                ));
            ?>
            <!--I want to display the product category name as a class to replace $category as shown below-->
            <div class="col-lg-4 col-sm-6 col-md-4 item-mgn portfolio-item <?php $category = get_the_category( $post->ID );
           echo $category[0]->cat_name;?>">
                <div class="project_thumb">
                     <?php the_post_thumbnail(); ?>
                </div>
                <div class="project_cont">
                    <a href="<?php the_permalink(); ?>">
                        <h3 class="boxeq"><?php the_title(); ?></h3>
                    </a>


                    <a href="<?php the_permalink(); ?>"><button class="btn-green">See Features</button></a>

                    <a href="#"><button class="btn-line">Acquire Property</button></a>
                </div>
            </div>


            <?php 
                endwhile;
                wp_reset_postdata(); 
            ?>  

        </div><!--portfolio-items-->

1 个答案:

答案 0 :(得分:1)

我终于能够解决这个问题。为了使那些面临可能遇到的相同问题的人,以下是供您仔细阅读的更新的工作代码。

<ul class="portfolio-filter">
        <li>
            <a class="active" href="#" data-filter="*">All</a>
        </li>
        <?php
            $args = array(
                'hide_empty'=> 0,
                'orderby' => 'name',
                'order' => 'ASC'
            );

           $product_categories = get_terms( 'product_cat', $cat_args );
             foreach ($product_categories as $key => $category) {
                echo 
                    '<li>
                        <a href="#" data-filter=".'.$category->name.'">    
                            '.$category->name.'
                        </a>
                    </li>';
            }
        ?>

    </ul>

    <div class="row">
        <div class="portfolio-items">

           <?php

                $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => -1, 'cat' => $category_id, 'orderby' =>'date','order' => 'DESC' );

                $loop = new WP_Query( $args );
                while ( $loop->have_posts()) : $loop->the_post(); global $product;
                if ( $attachments = get_children(
                    array(
                    'post_type' => 'attachment',
                    'post_mime_type'=>'image',
                    'numberposts' => 99,// -1 to get all images
                    'post_status' => null,
                    'post_parent' => $post->ID
                    )
                ));

                global $wp_query;
                $terms_post = get_the_terms( $post->cat_ID , 'product_cat' );
                foreach ($terms_post as $term_cat) { 

                }
            ?>

            <div class="col-lg-4 col-sm-6 col-md-4 item-mgn portfolio-item <?php echo $term_cat->name; ?>">
                <div class="project_thumb">
                     <?php the_post_thumbnail(); ?>
                </div>
                <div class="project_cont">
                    <a href="<?php the_permalink(); ?>">
                        <h3 class="boxeq"><?php the_title(); ?></h3>
                    </a>


                    <a href="<?php the_permalink(); ?>"><button class="btn-green">See Features</button></a>

                    <a href="#"><button class="btn-line">Acquire Property</button></a>
                </div>
            </div>


            <?php 
                endwhile;
                wp_reset_postdata(); 
            ?>  

        </div><!--portfolio-items-->   

    </div>