Documentation > Developers > Filter Examples > Add Random Background Image to Delivery Collections

Add Random Background Image to Delivery Collections

Please note: This only works for delivery collections!

function my_picu_delivery_random_collection_background( $custom_styles ) {

	$post = get_post();
	$delivery_option = get_post_meta( $post->ID, '_picu_collection_delivery_option', true );

	if ( $delivery_option == 'upload' AND ( 'delivery-draft' == $post->post_status OR 'delivered' == $post->post_status ) ) {

		$delivery_images = get_post_meta( $post->ID, '_picu_collection_delivery_ids', true );
		$image_ids = explode( ',', $delivery_images );
		shuffle( $image_ids );
		$image = wp_get_attachment_image_src( $image_ids[0], 'full' );

		if ( $image ) {
			$custom_styles .= 'body { background: url(' . $image[0] . ') fixed; background-size: cover; }';
		}

	}

	return $custom_styles;
}

add_filter( 'picu_custom_styles', 'my_picu_delivery_random_collection_background' );Code language: PHP (php)

Need help?

If you couldn’t find what you were looking for and need more assistance, please get in touch with us directly and we’re happy to help.