Skip Navigation

[Resolved] I want to create random background images with custom post type

This support ticket is created 6 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Author
Posts
#1127777

Tell us what you are trying to do?

I'm replicating a client's website. I want to create random header images that change when the page refreshes. I have created a custom post type for each image (6) with appropriate links to images and created a view. I'm using Astra theme & Elementor and came up with a few ways that might work.

1 Astra custom layout with wp_head hook.
RESULT: If I leave the shortcode in the editor, I get an image above the navigation, with no CSS. If I go to into Elemetor and add the shortcode within the header background image area in the shortcode widget, it does nothing.

2 I tried calling dynamic a background image in Elementor, that did nothing.
3 Add javacript to header with image links... here's what I tried, from a google search:

tried first: <script>
var bgs = [
'hidden link',
'hidden link',
'hidden link',
'hidden link',
'hidden link',
'hidden link',
'hidden link',
'hidden link',
'hidden link',
];

window.onload = function () {
var n = Math.floor(Math.random()*bgs.length);
document.body.style.backgroundImage = 'url(' + bgs[n] + ')';
}
</script>

no results

<script src="jquery-1.10.2.js" type="text/javascript"></script>
<script type="text/javascript">
var loading = function () {
var totalCount = 6;
var num = Math.floor(Math.random() * totalCount) + 1
document.body.background = 'images/' + num + '.jpg'

}
</script>

no results

CSS I used for my view of random header:

.eye_rndm_background {
width: 100%;
height: 50vh;
background-repeat: no-repeat;
background-size: cover;
background-color: #DBDBDB;
background-position: center center;
overflow: hidden;
}

Is there any documentation that you are following? I found javascript from a search that I tried also... sorry, not a developer and fumbling more than I should.

Is there a similar example that we can see? hidden link

What is the link to your site? here's what I'm getting as a result from the custom layout in Astra. hidden link

#1128144

Hi Laurie,

Thank you for contacting us and I'll be happy to assist.

From your message, I understand that you'd like your random background images to show on the homepage's section, shown in this screenshot:
hidden link

At the moment, the content coming from the view, at the top of the page, is always showing the same image.

I'll suggest checking if the "Random order" is selected in the view's "Ordering" setting and "Limit and Offset" is set to show 1 item.

In case the view is not showing the image randomly, I’ll need temporary access (WP-Admin and FTP) to your site, to see how the view and homepage are set.

This access will allow me to offer better help and check if some configurations or form code might need to be changed.

Your next answer will be private which means only you and our support team will have access to it.

If you’re going to share the access details for the live/production website, it is very important that a complete backup of your database and website has been made.

We would additionally need your permission to deactivate and reactivate Plugins and the Theme and to change configurations on the site. This is also a reason the backup is really important.

Please let me know if you have any further information or questions.

Regards,
Waqar

#1128545

Client decided they no longer want the random headers. I'm still curious about how to do it, but don't want you to waste your time.

Thank you!