• Firstly, I understand how to add an image behind a post title. What I don’t understand though, what I would like to know, is how one might set it so that this background image varies from post to post, so that one post might have an image of carrots as the background behind the title and then the next post might have an image of tomatoes.

    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. Or could it be done so that the bg image is given according to where a post sits (whether the first post on the page, the second, etc)?

    I searched but could not find an answer for this. Presumably though someone has already mapped out such a thing.

Viewing 4 replies - 1 through 4 (of 4 total)
  • 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

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to vary/randomize post title background image from entry to entry’ is closed to new replies.