WooCommerce产品滑块,具有项目计数

huangapple go评论53阅读模式
英文:

Woocommerece Products slider with item count

问题

我在谷歌上搜索了我想要的内容,但找不到任何满足我的需求的插件。

我需要在幻灯片中显示产品,当滑动项目时,项目计数会像附件中的 Pesta & Seafood (1-4 of 9) 旁边的那样更改。

WooCommerce产品滑块,具有项目计数

英文:

I searching in google about what i want but i can't found any plugin meet my needs

I need to display product in slider and when make slide item count change like in attachment beside Pesta & Seafood (1-4 of 9)

WooCommerce产品滑块,具有项目计数

答案1

得分: 1

这是使用Slick轮播的示例。
这是结果的预览 - https://prnt.sc/NNKZ-fPkCjDF
如何在项目中集成它 - https://kenwheeler.github.io/slick/

这是HTML结构

<div class="carousel-with-counter">
    <div class="counter">
        <span class="current"></span> of <span class="total"></span>
    </div>
    <div class="carousel">
        <div class="item"><img src="https://dummyimage.com/270x200/000/fff" /></div>
        <div class="item"><img src="https://dummyimage.com/270x200/000/fff" /></div>
        <div class="item"><img src="https://dummyimage.com/270x200/000/fff" /></div>
        <div class="item"><img src="https://dummyimage.com/270x200/000/fff" /></div>
        <div class="item"><img src="https://dummyimage.com/270x200/000/fff" /></div>
        <div class="item"><img src="https://dummyimage.com/270x200/000/fff" /></div>
        <div class="item"><img src="https://dummyimage.com/270x200/000/fff" /></div>
        <div class="item"><img src="https://dummyimage.com/270x200/000/fff" /></div>
        <div class="item"><img src="https://dummyimage.com/270x200/000/fff" /></div>
    </div>
</div>

这是JS代码

(function ($) {  
    $(document).ready(function() {
        carousel_with_counter();
    });
    function carousel_with_counter() {
        var $carousel = $('.carousel-with-counter .carousel');

        $carousel.on('init reInit afterChange', function(event, slick, currentSlide, nextSlide) {
            var totalSlides = slick.slideCount;
            var slidesToShow = slick.options.slidesToShow;
            var slideRangeStart = slick.slickCurrentSlide() + 1;
            var slideRangeEnd = Math.min(slideRangeStart + slidesToShow - 1, totalSlides);

            var slideRange = slideRangeStart + '-' + slideRangeEnd;

            $('.carousel-with-counter .counter .current').text(slideRange);
            $('.carousel-with-counter .counter .total').text(totalSlides);
        });

        $carousel.slick({
            slidesToShow: 4,
            slidesToScroll: 4,
            arrows: true,
            dots: false,
        });
    }
})(jQuery);

这是如何将其用作短代码的方法 - 短代码应如下所示 [carousel_with_counter ids="1,2,3,4"]

add_shortcode('carousel_with_counter','products_for_carousel');
function products_for_carousel($atts) {
    
    $product_ids = explode(",", $atts['ids']);

    $args = array(
        'post_type' => 'product',
        'posts_per_page' => -1,
        'post__in' => $product_ids
    );
    
    ob_start();
    $q = new WP_Query($args);

    if($q->have_posts()):
    echo '<div class="carousel-with-counter"><div class="counter"><span class="current"></span> of <span class="total"></span></div>';
        echo '<div class="carousel">';
            while($q->have_posts()): $q->the_post();
                echo '<div class="item">';
                    wc_get_template_part('content','product');
                echo '</div>';
            endwhile;
        echo '</div>';
    echo '</div>';
    endif;
    wp_reset_query();
    $content = ob_get_contents();
    ob_end_clean();
    return $content;
}

使用分类术语term_id的备选解决方案
这是如何将其用作短代码的方法 - 短代码应如下所示 [carousel_with_counter id="1"]

add_shortcode('carousel_with_counter','products_for_carousel');
function products_for_carousel($atts) {
    
    $category_id = $atts['id'];

    $args = array(
        'post_type' => 'product',
        'posts_per_page' => -1,
        'tax_query' => array(
          array(
           'taxonomy' => 'product_cat',
           'field' => 'term_id',
           'terms' => $category_id 
          )
        )
    );
    
    ob_start();
    $q = new WP_Query($args);

    if($q->have_posts()):
    echo '<div class="carousel-with-counter"><div class="counter"><span class="current"></span> of <span class="total"></span></div>';
        echo '<div class="carousel">';
            while($q->have_posts()): $q->the_post();
                echo '<div class="item">';
                    wc_get_template_part('content','product');
                echo '</div>';
            endwhile;
        echo '</div>';
    echo '</div>';
    endif;
    wp_reset_query();
    $content = ob_get_contents();
    ob_end_clean();
    return $content;
}
英文:

Here is example using Slick carousel
Here is preview of the result - https://prnt.sc/NNKZ-fPkCjDF
How to integrate it in your project - https://kenwheeler.github.io/slick/

This is the html structure

&lt;div class=&quot;carousel-with-counter&quot;&gt;
    &lt;div class=&quot;counter&quot;&gt;
        &lt;span class=&quot;current&quot;&gt;&lt;/span&gt; of &lt;span class=&quot;total&quot;&gt;&lt;/span&gt;
    &lt;/div&gt;
    &lt;div class=&quot;carousel&quot;&gt;
        &lt;div class=&quot;item&quot;&gt;&lt;img src=&quot;https://dummyimage.com/270x200/000/fff&quot; /&gt;&lt;/div&gt;
        &lt;div class=&quot;item&quot;&gt;&lt;img src=&quot;https://dummyimage.com/270x200/000/fff&quot; /&gt;&lt;/div&gt;
        &lt;div class=&quot;item&quot;&gt;&lt;img src=&quot;https://dummyimage.com/270x200/000/fff&quot; /&gt;&lt;/div&gt;
        &lt;div class=&quot;item&quot;&gt;&lt;img src=&quot;https://dummyimage.com/270x200/000/fff&quot; /&gt;&lt;/div&gt;
        &lt;div class=&quot;item&quot;&gt;&lt;img src=&quot;https://dummyimage.com/270x200/000/fff&quot; /&gt;&lt;/div&gt;
        &lt;div class=&quot;item&quot;&gt;&lt;img src=&quot;https://dummyimage.com/270x200/000/fff&quot; /&gt;&lt;/div&gt;
        &lt;div class=&quot;item&quot;&gt;&lt;img src=&quot;https://dummyimage.com/270x200/000/fff&quot; /&gt;&lt;/div&gt;
        &lt;div class=&quot;item&quot;&gt;&lt;img src=&quot;https://dummyimage.com/270x200/000/fff&quot; /&gt;&lt;/div&gt;
        &lt;div class=&quot;item&quot;&gt;&lt;img src=&quot;https://dummyimage.com/270x200/000/fff&quot; /&gt;&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

This is the js code

(function ($) {  
    $(document).ready(function() {
        carousel_with_counter();
    });
    function carousel_with_counter() {
        var $carousel = $(&#39;.carousel-with-counter .carousel&#39;);

        $carousel.on(&#39;init reInit afterChange&#39;, function(event, slick, currentSlide, nextSlide) {
            var totalSlides = slick.slideCount;
            var slidesToShow = slick.options.slidesToShow;
            var slideRangeStart = slick.slickCurrentSlide() + 1;
            var slideRangeEnd = Math.min(slideRangeStart + slidesToShow - 1, totalSlides);

            var slideRange = slideRangeStart + &#39;-&#39; + slideRangeEnd;

            $(&#39;.carousel-with-counter .counter .current&#39;).text(slideRange);
            $(&#39;.carousel-with-counter .counter .total&#39;).text(totalSlides);
        });

        $carousel.slick({
            slidesToShow: 4,
            slidesToScroll: 4,
            arrows: true,
            dots: false,
        });
    }
})(jQuery);

Here is how to use it as shortcode - shortcode should look like [carousel_with_counter ids=&quot;1,2,3,4&quot;]

add_shortcode(&#39;carousel_with_counter&#39;,&#39;products_for_carousel&#39;);
function products_for_carousel($atts) {
    
    $product_ids = explode(&quot;,&quot;, $atts[&#39;ids&#39;]);

    $args = array(
        &#39;post_type&#39; =&gt; &#39;product&#39;,
        &#39;posts_per_page&#39; =&gt; -1,
        &#39;post__in&#39; =&gt; $product_ids
    );
    
    ob_start();
    $q = new WP_Query($args);

    if($q-&gt;have_posts()):
    echo &#39;&lt;div class=&quot;carousel-with-counter&quot;&gt;&lt;div class=&quot;counter&quot;&gt;&lt;span class=&quot;current&quot;&gt;&lt;/span&gt; of &lt;span class=&quot;total&quot;&gt;&lt;/span&gt;&lt;/div&gt;&#39;;
        echo &#39;&lt;div class=&quot;carousel&quot;&gt;&#39;;
            while($q-&gt;have_posts()): $q-&gt;the_post();
                echo &#39;&lt;div class=&quot;item&quot;&gt;&#39;;
                    wc_get_template_part(&#39;content&#39;,&#39;product&#39;);
                echo &#39;&lt;/div&gt;&#39;;
            endwhile;
        echo &#39;&lt;/div&gt;&#39;;
    echo &#39;&lt;/div&gt;&#39;;
    endif;
    wp_reset_query();
    $content = ob_get_contents();
    ob_end_clean();
    return $content;
}

Alternative solution with taxonomy term_id
Here is how to use it as shortcode - shortcode should look like [carousel_with_counter id=&quot;1&quot;]

add_shortcode(&#39;carousel_with_counter&#39;,&#39;products_for_carousel&#39;);
function products_for_carousel($atts) {
    
    $category_id = $atts[&#39;id&#39;]);

    $args = array(
        &#39;post_type&#39; =&gt; &#39;product&#39;,
        &#39;posts_per_page&#39; =&gt; -1, //Since we query by taxonomy we may have alot of products feel free to restrict
        &#39;tax_query&#39; =&gt; array(
          array(
           &#39;taxonomy&#39; =&gt; &#39;product_cat&#39;,
           &#39;field&#39; =&gt; &#39;term_id&#39;,
           &#39;terms&#39; =&gt; $category_id 
          )
        )
    );
    
    ob_start();
    $q = new WP_Query($args);

    if($q-&gt;have_posts()):
    echo &#39;&lt;div class=&quot;carousel-with-counter&quot;&gt;&lt;div class=&quot;counter&quot;&gt;&lt;span class=&quot;current&quot;&gt;&lt;/span&gt; of &lt;span class=&quot;total&quot;&gt;&lt;/span&gt;&lt;/div&gt;&#39;;
        echo &#39;&lt;div class=&quot;carousel&quot;&gt;&#39;;
            while($q-&gt;have_posts()): $q-&gt;the_post();
                echo &#39;&lt;div class=&quot;item&quot;&gt;&#39;;
                    wc_get_template_part(&#39;content&#39;,&#39;product&#39;);
                echo &#39;&lt;/div&gt;&#39;;
            endwhile;
        echo &#39;&lt;/div&gt;&#39;;
    echo &#39;&lt;/div&gt;&#39;;
    endif;
    wp_reset_query();
    $content = ob_get_contents();
    ob_end_clean();
    return $content;
}

huangapple
  • 本文由 发表于 2023年5月29日 21:15:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定