http://wpgym.com/using-wordpress-thumbnails-as-a-css-background/
That works, but I have a problem with it, in that if you want to have posts WITHOUT a background, those posts would need to have a different width, height and padding, so a php if else statement would be needed, and I’m a newbie to php, so I don’t know how to do it.
And in case you get stuck trying to figure out what that site means, here it is in a nutshell:
This goes ABOVE the loop:
<?php
function wpgym_extract_post_thumb( $str ){
$matches = array();
$pattern = '/src=[\'"]?([^\'" >]+)[\'" >]/';
$match = preg_match( $pattern, $str, $matches );
return($matches[1]);
}
?>
And this goes INSIDE the loop (my edits, to make it a title background, tested, it works):
<?php $bg_image = extract_post_thumb( get_the_post_thumbnail() ); ?>
<h2 class="post-with-bg" style="background: url( <?php echo $bg_image;?> ) no-repeat top left;">
<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'themename' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
It would need an if else to change the h2’s “class=”post-with-bg”” to “class=”post-no-bg”” if it doesn’t find a post thumbnail image.
I figured it out, with some help from HERE
Inside the loop:
<?php $bg_image = extract_post_thumb( get_the_post_thumbnail() ); ?>
<?php if ($bg_image != '') { ?>
<h2 class="title-with-bg" style="background: url( <?php echo $bg_image;?> ) no-repeat top left;">
<?php }else{ ?>
<h2 class="title-no-bg">
<?php } ?>
<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'themename' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
</h2>
Lol, OK, I’m bored, so I looked for an EXACT resolution to your question “Is there a way to set this randomly, so that the title bg image for each post is chosen from one of, say, five choices.”
First, install this plugin and set it up per the instructions (it will make a link at the bottom of Settings on the dashboard).
Then edit the file randomImage.php, replacing this line (at the end)
echo '<img src="'.$filename.'" title="'.substr($image_filename,0,-4).'" alt="'.substr($image_filename,0,-4).'" height="'.$physHeight.'" width="'.$physWidth.'"/>';
with this:
echo ''.$filename.'';
Then do something like this in the loop:
<h2 class="post-title" style="background:url(<?php if (function_exists('generateRandomImgTag')) { generateRandomImgTag(); } ?>) no-repeat;"> title stuff </h2>
Of course, you’ll need to set up some other php stuff to make it appear in certain posts, like the first post, or just with certain categories or tags, but, like I said, I’m a newb, if else stuff baffles me.
Actually, just
echo $filename ;
works