Achim
asked 10 years ago

hello,
how can i modify the theme, so that the cover image is randomly changed.

1 Answers
Wilfred
answered 10 years ago

Hi Achim, To resolve this issue please follow my instruction here:
1. Create a child theme. Please follow the link below on how to create the child theme:   https://codex.wordpress.org/Child_Themes
2. Open up the functions.php file in the child theme folder that you have just created from Step 1.
3. Add the following code to file functions.php

<?php
// Cover
// ----------------------------------------------
if (!function_exists('custom_cover') ) {
function codex_custom_init() {
$args = array(
'public' => true,
'label' => 'Cover',
'supports' => array( 'title', 'thumbnail')
);
register_post_type( 'cover', $args );
}
add_action( 'init', 'codex_custom_init' );
function custom_cover() {
$args = array(
'post_type' => 'cover',
'posts_per_page' => 1,
'orderby' => 'rand'
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size');
if ($thumb_url[0]) {
?>
<style type="text/css">
.style-default .banner.cover {
background-image: url(<?php echo $thumb_url[0] ?>) !important;
}
.style-default .banner.cover:before {
opacity: .8 !important;
}
</style>
<?php
}
}
}
wp_reset_postdata();
}
add_action( 'wp_head', 'custom_cover' );
}

  4. Set featured image for the post of the Post type Cover. Please note that the cover images will randomly come from one of the posts under the post type cover. Take a look at the following screenshot: Hope this helps!

Powered by DW Question & Answer Pro