Randomize 1st image only in gallery

Jeffrey Johnson's Avatar

Jeffrey Johnson

23 Jan, 2018 05:55 PM

Hi, I have used Royal Slider on my WordPress site to show 3 images on my home page. Great! The client would like the first image to randomly load another image, so, instead of slides 1, 2, 3, we have slides 1A, 1B, 2, 3. Where 1A and 1B switch randomly.

I was able to get the slides to show randomly (231, 132, 123, 321, etc etc) -- but that is not what the client wants. Is there a way to ONLY have the first image to load? I have tried the following jquery method, but can't seem to find the right selector....please advise.

function getRandomImage()
{
    var images = [
        "http://dev.ico.edu/wp-content/uploads/2015/08/home-hero-mesner1.jpg",
        "http://dev.ico.edu/wp-content/uploads/2015/08/home-hero-dedicated_optometry.jpg"
    ];
    return images[Math.floor(Math.random() * 2)];
}

$(document).ready(function($){
    $(".rsImg, .rsMainSlideImage").attr('src', getRandomImage());
});

  1. Support Staff 1 Posted by Dmitry Semenov on 23 Jan, 2018 09:55 PM

    Dmitry Semenov's Avatar

    Hello,

    There is new_rs_before_js_init_code filter that allows to add JS code before slider is initialized. All child elements of .royalSlider element will be treated as a slide, you may modify them however you wish.

    function add_additional_rs_code() {
        ?>
        $('.royalSlider').each(function() {
            //  Slide elements
            var slideElements = $(this).children();
    
            // First slide element
            var firstSlide = slideElements.eq(0);
    
        });
        <?php
    }
    add_action('new_rs_before_js_init_code', 'add_additional_rs_code');
    

    Documentation of non-WordPress version might also help http://dimsemenov.com/plugins/royal-slider/documentation/#basic-usage (step #2 for markup)

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac