Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

How to change background image of page hero (elements) randomly

  • Hi,

    I asked a similar question some time ago. The main focus then was to randomly show page hero elements on specific pages. This time I need just the background image to change. In a perfect world I would not have to create new elements for each and every page. This would make things pretty messy. We have a header element for most of the 15 or so pages. Typically, there is some text overlay like on this page. I would like the images to change randomly after reloading the page. I could write a function that injects some CSS right before the closing HEAD tag. That wouldn’t be super pretty and still messy, but it would do the trick. Do you have a better idea? How would you do this? I don’t use the block editor. Please let me know, if the block editor would provide more functions regarding this specific topic. I try to avoid the block editor, but I don’t want to be stubborn. 🙂

    Thank you!

  • Hi there,

    This definitely requires a custom snippet, as the block editor does not support this feature by default.

    Perhaps the method outlined in the following topic post suits your requirements?
    https://generatepress.com/forums/topic/random-background-image-in-element-block-page-hero/#post-2275893

  • That would work. Would you approve this approach? There is probably no more elegant solution for this, I guess.

  • Personally I would use this approach if I needed to meet such a requirement. 🙂

  • Thanks, Alvind. If it’s good enough for you, it’s good enough for me. 🙂

    By the way, I don’t get any notifications of new posts. The Notify-me-checkbox is enabled, of course. I checked the Spam folder. Nothing there. I had the same problem some years ago and changed my email. That worked until recently.

  • You’re welcome!

    That’s odd. When the notify checkbox is enabled, you should receive an email. Let me know if you get a notification from this reply.

  • Strange. NOW, I got a notification from your reply.

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.