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.

Show header element randomly

  • I try to create a “pseudo slider” so that after each reload of the front page 1 of 3 header elements is chosen randomly. I used Tom’s code.

    add_filter('generate_element_display', function ($display, $element_id) {
        $el_array = array(124, 136, 140);
        $rand_element = array_rand($el_array, 1);
        if (is_front_page() && $el_array[$rand_element] == $element_id) {
            $display = true;
        }
        return $display;
    }, 10, 2);

    No caching is active, but it doesn’t work. I checked the element IDs twice. I am obviously missing something.

  • Hi there,

    can i see the page where its not working ?

  • Hi David,
    sure. I put the URL in the private information field, since the site is still in development.

    Thanks, Mike

  • Hum, the code looks correct, it should work.

    Is this one of the random header elements?
    https://www.screencast.com/t/xXTNw4lM
    Let me know 🙂

  • Hi Ying,
    yes, this is one of the 3 elements. Database seems to be ok. Here are the elements with their IDs:

  • If the one I see is one of the 3 header elements, how can I know the function isn’t working?

    Let me know!

  • After reloading the page, it should show randomly the other elements, shouldn’t it? After deactivating the first element, the second header is shown. But it never changes randomly. Am I missing anything?

  • Hi Mkjj,

    Would the Page Heros be totally different in design and text, or is it just the image? Can you share a screenshot of all the 3 Headers for reference?

  • Hi Fernando, both the image and the text should change. I’ve just created temporary pages that show the other versions. Links are in the private information field.

    Sorry, posted the wrong links. I updated the links.

  • Do these 3 images correspond to the text shown on your sample links? Or, would they randomly be shuffled as well? For instance, Header 1 Text with Header 2 BG Image?

  • Image and text are always combined: Image 1 corresponds to text 1, image 2 to text 2, image 3 to text 3.

  • Hi David,

    ok, I changed the snippet to this one and removed all display rules:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        
        // Your arry of element IDs
        $el_array = array( 124, 136, 140 );
        
        $rand_element = array_rand( $el_array, 1 );
        
        if ( is_front_page() && $el_array[ $rand_element ] == $element_id ) {
            $display = true;
        }
     
        return $display;
        
    }, 10, 2 );

    Now, it does cycle through the header elements. But sometimes no header at all is loaded. Which is not so ideal. 🙂

    I do understand if this is beyond the scope of the support forum, and I don’t want to bother you with such a specific issue. It might use a little bit too much of your precious time. It really is ok for me, if we can’t find a working solution for this.

  • Ok, try creating a new header element, which has its display rules set.
    This will be the fallback one in case there is no element displayed.

    to note: when you have many header elements all set to display in the same location, GP will display the “oldest” one. So in theory as long as this new header element is “newest” it will only show if none of the random ones get displayed.

  • Wow! That totally works! Would have never thought of this Thank you very much for your effort!

  • Awesome – glad to hear that!

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