Skip Navigation

[Resolved] random background image

This thread is resolved. Here is a description of the problem and solution.

Problem:

I want to create a random background image for a div that changes every time the page loads. I am using classic Toolset views and a Genesis child theme.

Solution:

- Create an image custom field and make it a repeating field with the slug set to "slider-images."

- Create a shortcode as described in the documentation link below.

- To use the random image as a background, add custom HTML code inside the content template or wherever you want to use the shortcode, with the following format:

<div class="background-container" style="background-image: URL([types field='slider-images' index="[generate-rand-num field='slider-images' output='raw']"][/types])">
Content of the DIV
</div>

Relevant Documentation:

https://toolset.com/forums/topic/choose-random-image-from-custom-field/

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 0.08 hours from now. Thank you for your understanding.

This topic contains 2 replies, has 2 voices.

Last updated by Steve 1 year, 9 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2563129

My client would like to have a random background image appear every time the page loads. Is it possible I could make a cpt for them to upload images to and then somehow create a view that uses a single random img src as a background image for a div?

The site uses classic toolset views and the theme is a genesis child.

#2563599

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

I suggest that you used the method below:

https://toolset.com/forums/topic/choose-random-image-from-custom-field/

So basically you will create an image custom field and make it a repeating field and set the slug of that field to slider-images

Then you will create a shortcode with the details mention and it will show the random image that you add in side the repeating image field. You can ad as many images as you want in the repeating image field.

To make it a background you will need to add a custom HTML code inside the content template or wherever you want to use the shortcode and add something like this:

<div class="background-container" style="background-image: URL([types field='slider-images' index="[generate-rand-num field='slider-images' output='raw']"][/types])">
Content of the DIV
</div>

I did change the TYPES shortcode to include the raw output to show the URL only and not the image itself.

Thanks.

#2564225

Thanks so much, that should be perfect for what i need

Really appreciate the help