假设图片都上传到了页面ID为1740的页面。

<?php
$query = new WP_Query( array(
	'post_type' => 'page',
	'page_id' => 1740
) );
if ( $query->have_posts() ):
	$images = [];
while ( $query->have_posts() ): $query->the_post();

$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts( $args );
if ( $attachments ) {
	foreach ( $attachments as $attachment ) {
		// get url from Plugin: WP Gallery Custom Links
		$image_href = ( !empty( get_post_meta( $attachment->ID, '_gallery_link_url', true ) ) ) ? get_post_meta( $attachment->ID, '_gallery_link_url', true ) : '';
		// get attachment
		$image_url = ( !empty( wp_get_attachment_url( $attachment->ID ) ) ) ? wp_get_attachment_image_src( $attachment->ID, 'large', true ) : '';
		$image_alt = ( !empty( get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ) ) ) ? get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ) : '';
		$images[] = array(
			'url' => $image_url[ 0 ],
			'alt' => $image_alt,
			'href' => $image_href
		);
	}
}
endwhile;
$image = $images[ array_rand( $images, 1 ) ];
?>
<a href = "<?php echo $image['href']; ?>" title = "<?php echo $image['alt']; ?>" >
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>"/>
</a>
<?php
wp_reset_postdata();
endif;
?>