Owl slider adds class "last" to every 4th item in slider

This topic has 19 replies, 2 voices, and was last updated 7 years, 10 months ago ago by Eva Kemp

  • Avatar: Royalty
    Royalty
    Participant
    January 7, 2017 at 13:54

    Hello,
    I noticed a weird behavior of owl slider concerning the “first” and “last” classes. I am using the [etheme_products] shortcode to display products by ID. My goal is to border the first slide. I was happy to find that the first slide has it’s own class addition called “first” and added the border successfully via my childtheme’s style.css:

    .slide-item  .first  {
    border: 1px solid #888 !important;	
    }

    But the border was not only added to the first slide, but also to the 5th. And the 4th item has the class “last”.

    I noticed this behavior not only on my page but also on your demo site in related products section: preview.themeforest.net/item/royal-multipurpose-wordpress-theme/full_screen_preview/8611976

    This means that this has nothing to do with my childtheme or any plugins (so there is no need to give you my credentials), its Royal theme that says somewhere in it’s code that the first item gets the class “first” and every 4th item gets the class “last”, even if there are more than 4 items in the slider.

    Could you please tell me what to do in order to style only the first item in the owl slider different from the other items?

    Many thanks in advance and best wishes!

    18 Answers
    Avatar: Royalty
    Royalty
    Participant
    January 7, 2017 at 14:43

    I think I found the part that causes the problem:
    In content-product-slider.php there is this part:

    // Extra post classes
    $classes = '';
    if ( 0 == ( $woocommerce_loop['loop'] - 1 ) % $woocommerce_loop['columns'] || 1 == $woocommerce_loop['columns'] )
    	$classes .= 'first ';
    if ( 0 == $woocommerce_loop['loop'] % $woocommerce_loop['columns'] )
    	$classes .= 'last ';

    When I edit it to the following, it seems to work but I dont know if this is a proper solution or if this will cause any problems…?

    // Extra post classes
    $classes = '';
    if ( 0 == ( $woocommerce_loop['loop'] - 1 ) )
    	$classes .= 'first ';
    if ( 0 == $woocommerce_loop['loop'] % $woocommerce_loop['columns'] )
    	$classes .= 'last ';
    Avatar: Royalty
    Royalty
    Participant
    January 7, 2017 at 14:52

    Ok sorry its me again,
    also in content-product-slider.php there is this part that says that there should be 4 columns:

    // Store column count for displaying the grid
    if ( empty( $woocommerce_loop['columns'] ) )
    	$woocommerce_loop['columns'] = apply_filters( 'loop_shop_columns', 4 );

    What is this code for and how can I edit it in order to set the colums dynamically?

    Avatar: Eva
    Eva Kemp
    Support staff
    January 11, 2017 at 11:33

    Hello,

    Sorry for a delay.
    That code is for Products element in VC editor. Columns option is appeared when Grid is selected http://storage5.static.itmages.com/i/17/0111/h_1484130717_9812041_813dab7a45.jpeg .

    Regards,
    Eva Kemp.

    Avatar: Royalty
    Royalty
    Participant
    January 13, 2017 at 11:39

    Thank you, but I don’t want to use grid, I want to use slider…

    Avatar: Eva
    Eva Kemp
    Support staff
    January 13, 2017 at 14:56

    Hello,

    From your previous message I saw you resolved the problem, didn’t you?

    Regards,
    Eva Kemp.

    Avatar: Royalty
    Royalty
    Participant
    January 15, 2017 at 18:20

    No, it’s very confusing.
    If I add the shortcode [etheme_products] to a page, classes are set like this:
    1. Item class FIRST
    2. Item
    3. Item
    4. Item class LAST
    5. Item class FIRST
    […]

    But if I add the same shortcode on product description field of a product, the classes are set like this:
    1. Item
    2. Item
    3. Item class LAST
    4. Item class FIRST
    5. Item
    […]

    When I add two shortcodes like [etheme_products][etheme_products], the first slider has the classes set like my second example and the second slider has the classes set like the first example.

    I really don’t get it, its totally confusing. I would like to ignore all of these confusing classes and just find a way to style the first item of the slider, but I dont know how. :first-child doesnt work here…

    Avatar: Eva
    Eva Kemp
    Support staff
    January 17, 2017 at 16:19

    Hello,

    You can use sliders ids, they are unique and use css code for each of them.
    Also you can select how many items to show in product slider settings http://prntscr.com/dwp5un .

    Regards,
    Eva Kemp.

    Avatar: Royalty
    Royalty
    Participant
    January 17, 2017 at 16:52

    Where can I set the slider id? I dont find this option in the slider-settings…

    Avatar: Royalty
    Royalty
    Participant
    January 17, 2017 at 16:55

    Or do you mean that I should use the ids that are automatically generated like slider-8637?
    Thats no solution as I need these sliders on every product page…

    Avatar: Royalty
    Royalty
    Participant
    January 17, 2017 at 16:57

    I just need to understand why this part results in these weird classes

    // Store loop count we're currently on
    if ( empty( $woocommerce_loop['loop'] ) )
    	$woocommerce_loop['loop'] = 0;
    
    // Store column count for displaying the grid
    if ( empty( $woocommerce_loop['columns'] ) )
    	$woocommerce_loop['columns'] = apply_filters( 'loop_shop_columns', 4 );
    
    // Ensure visibility
    if ( ! $product || ! $product->is_visible() )
    	return;
    
    // Increase loop count
    $woocommerce_loop['loop']++;
    
    // Extra post classes
    $classes = '';
    if ( 0 == ( $woocommerce_loop['loop'] - 1 ) % $woocommerce_loop['columns'] || 1 == $woocommerce_loop['columns'] )
    	$classes .= 'first ';
    if ( 0 == $woocommerce_loop['loop'] % $woocommerce_loop['columns'] )
    	$classes .= 'last ';

    I just want the first item in the slider to be marked with “first” or something else, thats all.

    Avatar: Eva
    Eva Kemp
    Support staff
    January 19, 2017 at 11:04

    Hello,

    You need do the following:
    – go to framework/theme-functions.php
    – find etheme_create_slider function
    – copy it to child theme without if(!function_exists('etheme_create_slider')) {
    – add to it this code http://prnt.sc/dxj40b
    $first_slide_class = ( $_i === 1 ) ? 'your-first-slide-class' : '';
    – and you’ll receive this result http://prnt.sc/dxj4p2

    Regards,
    Eva Kemp.

    Avatar: Royalty
    Royalty
    Participant
    January 19, 2017 at 18:45

    Thank you, I did what you said, but the change has no effect, it doesnt add the class.
    Here is the code that I added to my childtheme’s functions.php

        function etheme_create_slider($args, $slider_args = array()){//, $title = false, $shop_link = true, $slider_type = false, $items = '[[0, 1], [479,2], [619,2], [768,4],  [1200, 4], [1600, 4]]', $style = 'default'
            global $wpdb, $woocommerce_loop;
            
            $product_per_row = etheme_get_option('prodcuts_per_row');
            extract(shortcode_atts(array( 
    	        'title' => false,
    	        'shop_link' => false,
    	        'slider_type' => false,
    	        'items' => '[[0, 1], [479,2], [619,2], [768,4],  [1200, 4], [1600, 4]]',
    	        'style' => 'default',
    	        'block_id' => false
    	    ), $slider_args));
    
            $box_id = rand(1000,10000);
            $multislides = new WP_Query( $args );
            $shop_url = get_permalink(woocommerce_get_page_id('shop'));
            $class = $container_class = $title_output = '';
            if(!$slider_type) {
            	$woocommerce_loop['lazy-load'] = true;
            	$woocommerce_loop['style'] = $style;
            }
            
    
            if(!$slider_type) {
                $container_class = '';
                $class .= 'owl-carousel';
            } elseif($slider_type == 'swiper') {
                $container_class = 'et-swiper-container';
                $class .= 'swiper-wrapper';
            }
    
            if ( $multislides->have_posts() ) :
                if ($title) {
                    $title_output = '<h2 class="title"><span>'.$title.'</span></h2>';
                }   
                  echo '<div class="carousel-area '.$container_class.' slider-'.$box_id.'">';
                      echo $title_output;
                      echo '<div class="'.$class.' productCarousel">';
                            $_i=0;
                        	if($block_id && $block_id != '' && et_get_block($block_id) != '') {
                                echo '<div class="slide-item '.$slider_type.'-slide">';
                                    echo et_get_block($block_id);
                                echo '</div><!-- slide-item -->';
                        	}
                            while ($multislides->have_posts()) : $multislides->the_post();
                                $_i++;
                                $first_slide_class = ( $_i === 1 ) ? 'your-first-slide-class' : '';
                                if(class_exists('Woocommerce')) {
                                    global $product;
                                    if (!$product->is_visible()) continue; 
                                    echo '<div class="slide-item product-slide '.$slider_type.'-slide">';
                                        woocommerce_get_template_part( 'content', 'product-slider' );
                                    echo '</div><!-- slide-item -->';
                                }
    
                            endwhile; 
                      echo '</div><!-- products-slider -->'; 
                  echo '</div><!-- slider-container -->'; 
            endif;
            wp_reset_query();
            unset($woocommerce_loop['lazy-load']);
            unset($woocommerce_loop['style']);
            
            if($items != '[[0, 1], [479,2], [619,2], [768,4],  [1200, 4], [1600, 4]]') {
                $items = '[[0, '.$items['phones'].'], [479,'.$items['tablet'].'], [619,'.$items['tablet'].'], [768,'.$items['tablet'].'],  [1200, '.$items['notebook'].'], [1600, '.$items['desktop'].']]';
            } 
            if(!$slider_type) {
    	        echo '
    	
    	            <script type="text/javascript">
    	                jQuery(".slider-'.$box_id.' .productCarousel").owlCarousel({
    	                    items:4, 
    	                    lazyLoad : true,
    	                    navigation: true,
    	                    navigationText:false,
    	                    rewindNav: false,
    	                    itemsCustom: '.$items.'
    	                });
    	
    	            </script>
    	        ';
            } elseif($slider_type == 'swiper') {
    	        echo '
                    <script type="text/javascript">
                      if(jQuery(window).width() > 767) {
                          jQuery(".slider-'.$box_id.'").etFullWidth();
                          var mySwiper'.$box_id.' = new Swiper(".slider-'.$box_id.'",{
                            keyboardControl: true,
                            centeredSlides: true,
                            calculateHeight : true,
                            slidesPerView: "auto",
                            mode: "horizontal"
                          })
                      } else {
                          var mySwiper'.$box_id.' = new Swiper(".slider-'.$box_id.'",{
                            calculateHeight : true
                          })
                      }
                    
                        jQuery(function($){
                            $(".slider-'.$box_id.' .slide-item").click(function(){
                                mySwiper'.$box_id.'.swipeTo($(this).index());
                                $(".lookbook-index").removeClass("active");
                                $(this).addClass("active");
                            });
                            
                            $(".slider-'.$box_id.' .slide-item a").click(function(e){
                                if($(this).parents(".swiper-slide-active").length < 1) {
                                    e.preventDefault();
                                }
                            });
                        }, jQuery);
                    </script>
    	        ';
            }
                
        }
    Avatar: Royalty
    Royalty
    Participant
    January 19, 2017 at 18:48

    Even if I add
    $first_slide_class = ( $_i === 1 ) ? 'your-first-slide-class' : '';
    directly to theme-functions, it has no effect.

    Avatar: Eva
    Eva Kemp
    Support staff
    January 19, 2017 at 19:00

    Hello,

    Please provide FTP credentials.

    Regards,
    Eva Kemp.

    Avatar: Royalty
    Royalty
    Participant
    January 19, 2017 at 19:11

    Ok, please find them in private content. Thank you.

    Please, contact administrator
    for this information.
    Avatar: Eva
    Eva Kemp
    Support staff
    January 20, 2017 at 15:26

    Hello,

    It was added . Please check .
    You had to do this way http://prnt.sc/dxj40b and not this http://prnt.sc/dy204b .

    Regards,
    Eva Kemp.

    Avatar: Royalty
    Royalty
    Participant
    January 20, 2017 at 17:02

    Ah ok I didnt see the second part! Thank you very much! Could you please delete the link to my testpage and the screenshot? Thank you and have a nice weekend!

    Avatar: Eva
    Eva Kemp
    Support staff
    January 21, 2017 at 11:02

    Hello,

    Done. You’re welcome.

    Regards,
    Eva Kemp.

  • Viewing 19 results - 1 through 19 (of 19 total)

The issue related to '‘Owl slider adds class "last" to every 4th item in slider’' has been successfully resolved, and the topic is now closed for further responses

8theme customization service
We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.