• Resolved C. Nicole

    (@c-nicole)


    Hi, first of all – thanks for this great plugin, my absolute favourite one!

    One question: Is it possible just to show a random first image and leave the rest in the fixed order?

    Best regards,
    Nicole

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi,

    Thank you for liking our plugin. What you’re asking is not possible with MetaSlider right out of the box but MetaSlider has hooks available to make such custom changes.

    However, you may need some extra coding in order for you to achieve what you want. https://www.metaslider.com/metaslider_type_slider_javascript/

    I’ve made a custom code that would work similar on what you want, but you still have to make some changes to update the code to work on your end. I’ve added some comments on what you need to change. You just have to put this on your theme’s function.php file.

    function metaslider_random_images($attributes, $slide, $slider_id) {
    	// change 123 to your slider id
    	// and change 234 to your first slide's id
    	if (($slider_id == 123) && ($slide['id']==234)) {
    		$images = array(
    			'image url here',
    			'image url here',
    			'image url here',
    		);
    		$attributes['src'] = $images[rand(0,2)];
    	}
    	return $attributes;
    }
    
    // use this filter if you're using flex slider
    add_filter('metaslider_flex_slider_image_attributes', 'metaslider_random_images', 10, 3);
    
    // use this filter if you're using r.slides
    add_filter('metaslider_responsive_slider_image_attributes', 'metaslider_random_images', 10, 3);

    Hope this helps!

    • This reply was modified 3 years, 8 months ago by Arreane.
    Thread Starter C. Nicole

    (@c-nicole)

    Thanks!
    Unfortunately it does not work yet.
    Is the first slide’s id the id of the image in the media library? Have not found an id for an image in the slider itself.

    Have inserted the code into the functions.php of my child theme – guess this should work?

    Best regards,
    Nicole

    Thread Starter C. Nicole

    (@c-nicole)

    'image url here',
    'image url here',
    (…)

    Should here be entered a list of all image Url the slider uses?

    Should here rand(0,2)
    and here 'metaslider_random_images', 10, 3)
    something be changed?

    Sorry for asking probably obvious things …

    Nicole

    Hi,

    I’m afraid you might need basic PHP skills in order to implement the customization properly.

    You can try to run var_dump() or print_r() functions to check the slide id’s value. For example: var_dump($slide);die;

    rand(0,2) is a PHP function that returns a random value. In this case ranging from 0 to 2
    0 -> is the index of the array’s first element.
    2 -> is the index of the array’s last element. If you have 10 image in the array, then the value in the rand() function should be 9.

    'metaslider_random_images', 10, 3) <- you don’t need to make changes on this part since this line is just used to call the WordPress hook

    Hope this makes sense. Let us know if you need anything else.

    Thread Starter C. Nicole

    (@c-nicole)

    Thanks ??
    This is my current code, but slider always starts with the same image.

    function metaslider_random_images($attributes, $slide, $slider_id) {
    
    	if (($slider_id == 5078) && ($slide['id']==5110)) {
    		$images = array(
    			'https://….jpg',
    			'https://….jpg',
    			'https://….jpg',
    			'https://….jpg,
                             (etc. all image urls)
    
    		);
    		$attributes['src'] = $images[rand(0,21)];
    	}
    	return $attributes;
    }
    
    add_filter('metaslider_responsive_slider_image_attributes', 'metaslider_random_images', 10, 3);

    I don’t want to stress you out so much, but maybe you have another tip?

    Nicole

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Just first slider image random?’ is closed to new replies.