Created
July 19, 2023 10:55
-
-
Save florianziegler/ba9fd941e24c4a795c661d17fd7bb160 to your computer and use it in GitHub Desktop.
Use a random picu collection image as background
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Use a random collection image as background. | |
* | |
* @param string $custom_styles CSS code; Make sure to add your code to the string and not overwrite it. | |
*/ | |
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 ) ) { | |
$images = get_post_meta( $post->ID, '_picu_collection_delivery_ids', true ); | |
} | |
elseif ( 'draft' == $post->post_status OR 'sent' == $post->post_status OR 'approved' == $post->post_status ) { | |
$images = get_post_meta( $post->ID, '_picu_collection_gallery_ids', true ); | |
} | |
if ( ! empty( $images ) ) { | |
$image_ids = explode( ',', $images ); | |
shuffle( $image_ids ); | |
$image = wp_get_attachment_image_src( $image_ids[0], 'thumbnail' ); | |
} | |
if ( ! empty( $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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment