如何修复 Woocommerce 中特色产品的 bootstrap carousal

How to fix bootstrap carousal for featured product in Woocommerce

我尝试了几个插件来为我的 woocommerce wordpress 网站提供特色产品滑块轮播,但它们没有按我的预期工作,所以我尝试自己创建。除了显示根本不存在的其他帖子外,它几乎可以正常工作。它们甚至不作为产品存在。第一张图片显示了特色产品。第二张图片是根本不存在的额外内容。此类不必要帖子的数量等于特色产品总数的倍数。关于类似问题的建议几乎反映了我所做的,但它有问题。在我的例子中,我有 5 个特色产品,它显示了 25 个不必要的帖子。目前,我尝试一次只显示一个项目,在解决此问题后,我将一次显示 3 个帖子,因此它会循环两次,让 6 个帖子可以滑动。

<div id="featured" class="carousel slide ">
                            <div class="carousel-inner ">
                            <?php
                            $args = array( 'post_type' => 'product',
                                           'meta_key' => '_featured',
                                           'meta_value' => 'yes',
                                           'posts_per_page' => 8,
                                           'post_status'     => 'publish',
                                           'offset'          => 0,
                                           'numberposts'     => 6,
                                           //'orderby' =>'rand',
                                           'order' => 'DESC' 
                                           );
                            $featured_loop = new WP_Query( $args );
                            //echo "<pre>";
                            //print_r($featured_loop);
                            //echo "</pre>";

                            if ( $featured_loop->have_posts()){

                                $i = 1; $count;
                                for ($count=0; $count < 6;) { 

                                    foreach ( $featured_loop as $featured ) {
                                    $featured_loop->the_post();
                                    ?>
                                    <div class=
                                      <?php
                                        echo '"';
                                        echo 'item '; 
                                        if ($i == 1) {
                                          echo 'active';
                                        }

                                        echo '"';

                                        ?>>

                                        <div class="col-xs-6 col-sm-4 col-md-4 col-lg-4 ">
                                                <div class="thumbnail">
                                                <i class="tag"></i>
                                                <a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                                                <?php 

                                                    if (has_post_thumbnail( $featured->post->ID )){
                                                            echo get_the_post_thumbnail($featured->post->ID, 'shop_catalog');
                                                        }
                                                        else {
                                                        echo '<img width ="150" src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" class="img-responsive img-rounded" />';
                                                        }

                                                ?>
                                                </a>
                                                </div><!-- thumbnail -->
                                                <div class="panel-body text-center">
                                                        <h6><?php the_title(); ?> </h6>                         
                                                </div><!-- panel-body text-center -->

                                            </div><!-- col-xs-6 col-sm-4 col-md-4 col-lg-4 -->
                                        </div>
                                    <?php

                                    $i++;


                                    }
                                    $count++;

                                }

                            }


                            ?>








                                </div><!-- carousal item class ends -->


                            </div><!-- carousal inner ends -->
                            <a class="left carousel-control" href="#featured" data-slide="prev"><i class="fa fa-arrow-left"></i></a>
                            <a class="right carousel-control" href="#featured" data-slide="next"><i class="fa fa-arrow-right"></i></a>  

                            <?php wp_reset_postdata(); wp_reset_query(); ?>
                        </div><!-- carousel slide -->  

首先,你的迭代是错误的。你首先 运行 一个畸形的 for 循环 n= 6 次,然后对于从 0 到 5 的每个整数你 运行 一个 foreach 循环 m 次,导致复杂度为 O(n*m) 的畸形循环无法解决您的问题。

这是您的代码,已重写。希望对你有帮助。

<div id="featured" class="carousel slide ">
    <div class="carousel-inner ">
        <?php
        $args          = array(
            'post_type'      => 'product',
            'meta_key'       => '_featured',
            'meta_value'     => 'yes',
            'posts_per_page' => 6,
            'post_status'    => 'publish',
            'offset'         => 0,
            'order'          => 'DESC'
        );
        $featured_loop = new WP_Query( $args );
        if ( $featured_loop->have_posts() ):
            while ( $featured_loop->have_posts() ) : $featured_loop->the_post(); ?>
                <div class="<?php echo 'item'; ?>">
                    <div class="col-xs-6 col-sm-4 col-md-4 col-lg-4 ">
                        <div class="thumbnail">
                            <i class="tag"></i>
                            <a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                                <?php echo woocommerce_get_product_thumbnail(); ?>
                            </a>
                        </div>
                        <div class="panel-body text-center">
                            <h6><?php the_title(); ?> </h6>
                        </div>
                    </div>
                </div>
            <?php endwhile; ?>
            <a class="left carousel-control" href="#featured" data-slide="prev"><i class="fa fa-arrow-left"></i></a>
            <a class="right carousel-control" href="#featured" data-slide="next"><i class="fa fa-arrow-right"></i></a>
            <?php wp_reset_postdata(); endif; ?>
    </div>
</div>
<?php wp_reset_query(); ?>

在@Sorin Gheata 的帮助下回答这个问题。他忘了让它作为 bootstrap 轮播。

<div id="featured" class="carousel slide ">
    <div class="carousel-inner ">
        <?php
        $args          = array(
            'post_type' => 'product',
        'meta_key' => '_featured',
        'meta_value' => 'yes',
        'numberposts'     => 6,
        'posts_per_page' => 6
        );
        $featured_loop = new WP_Query( $args );//global $product;
        if ( $featured_loop->have_posts() ):
            while ( $featured_loop->have_posts() ) : $featured_loop->the_post(); ?>
                                        <div class=
                                      <?php
                                        echo '"';
                                        echo 'item '; 
                                        if ($i == 1) {
                                          echo 'active';
                                        }

                                        echo '"';

                                        ?>>
                    <div class="col-xs-6 col-sm-4 col-md-4 col-lg-4 ">
                        <div class="thumbnail">
                            <i class="tag"></i>
                            <a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                                <?php echo woocommerce_get_product_thumbnail(); ?>
                            </a>
                        </div>
                        <div class="panel-body text-center">
                            <h6><?php the_title(); ?> </h6>
                        </div>
                    </div>
                </div>

            <?php  $i++; endwhile; ?>
            <a class="left carousel-control" href="#featured" data-slide="prev"><i class="fa fa-arrow-left"></i></a>
            <a class="right carousel-control" href="#featured" data-slide="next"><i class="fa fa-arrow-right"></i></a>
            <?php wp_reset_postdata(); endif; ?>
    </div>
</div>
<?php wp_reset_query(); ?>