I am trying to write a custom page type that gives a page a random background in WordPress. (i.e nice big image on the homepage). I have got it working! But am returning a large image, any ideas how to quickly add make it display the full image size URL?
Help much appreciated!
(I have stuck the full code in so anyone can use it on their own sites if it helps?)
<?php
/**
* Template Name: Fullscreen Random
*/
get_header();
?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<style media="screen" type="text/css">
body {
background: url(<?php
$args = array( 'post_type' => 'attachment', 'numberposts' => 1, 'orderby' => 'rand', 'post_status' => null, 'post_parent' => 5 );
$attachments = get_posts( $args );
if ($attachments) {
foreach ( $attachments as $attachment ) {
echo $attachment->guid;
}
}
?>) no-repeat center center fixed;
webkit-background-size: cover;
moz-background-size: cover;
o-background-size: cover;
background-size: cover;
}
#menu {background:rgba(0, 0, 0, 0.2)!important;}
* {color:white!important;}
</style>
<?php endwhile; ?>
<? get_footer(); ?>
Using wp_attachment_is_image to make sure you’re getting only your images and wp_get_attachment_image_src with the attachment id and “full” as parameters you can get the url for the full-size attachment image from the first element of the returned array (haven’t tested this but it seems viable).