Site logo

Archived topic

pulling random images for header element

3 replies · Started by Drew on July 23, 2019

Viewing posts 1–4 of 4

I've been following the thread at https://generatepress.com/forums/topic/variable-random-background-image-page-headers/page/2/ and implemented the final version of the solution Tom presented. Here's the snippet I'm running:

add_filter( 'generate_page_header_options', function( $options ) {
    if ( is_front_page() ) {
        $ids = array(
            '247',
            '246',
            '22873'
        );

        $id = $ids[array_rand($ids)];

        $options['image_id'] = $id;
    }

    return $options;
} );

Unfortunately, it isn't having any impact on the image display: http://morikamidev.wpengine.com/

This is a WPE hosted install and I do have Allow ORDER BY RAND() enabled. If I'm following the previous thread, I'm not entirely sure if that snippet is designed to pull a random header element or just random images for a single element (I'm after the latter).

Do you see any obvi errors in the snippet or is there a better approach?

Hi there,

That's for the old Page Headers.

Try this:

add_filter( 'generate_page_hero_background_image_url', function( $url ) {
    if ( is_front_page() ) {
        $urls = array(
            'https://yourURLtoImage.com/image.png',
            'https://yourURLtoImage.com/image2.png',
            'https://yourURLtoImage.com/image3.png'
        );

        $url = $urls[array_rand($urls)];
    }

    return $url;
} );

Let me know :)

Bingo, that does the trick, many thanks Tom :)

You're welcome :)

This archived topic is closed to new replies.