I create the gallery meta box with ACF gallery field and uploading the images to my pages. At frontend I shall want one random image from those gallery images.
Here is the steps.
PHP Snippet
At first you would copy the following PHP code and paste into the Code Snippet plugin. Or you can use my Customize Oxygen Builder plugin and put the code inside the includes/helpers.php file.
/** * Getting the random image URL from ACF gallery */ function paulc_get_random_image( $acf_field_key ) { if( ! function_exists( 'get_field' ) ) return; $getImages = get_field( $acf_field_key ); $size = 'full'; if( $getImages ) { $totalImages = count( $getImages ); $randomNumber = rand( 0, ( $totalImages - 1 ) ); if( $getImages[$randomNumber] ) return wp_get_attachment_image_url( $getImages[$randomNumber]['id'], $size ); } return; //* return a fallback image url }