[Resolved] pulling random images for header element

Home Forums Support [Resolved] pulling random images for header element

Home Forums Support pulling random images for header element

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #965423
    drew

    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?

    #965489
    Tom
    Lead Developer
    Lead Developer

    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 πŸ™‚

    #965914
    drew

    Bingo, that does the trick, many thanks Tom πŸ™‚

    #966181
    Tom
    Lead Developer
    Lead Developer

    You’re welcome πŸ™‚

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