Support » Developing with WordPress » code to pull uploaded photo into css background

  • Working on a site where the images are in a column with overflow:hidden. I’m trying to find a way to have the images be cut off from the center instead of the right side — but the only way is to have the image be in the background of a div.

    I’m thinking of adding something like this to the template:
    <div class=”genericphotostyle” style=”background-image:url(GRAB THE IMAGE URL DYNAMICALLY) blah blah”><img=”GRAB THE IMAGE URL DYNAMICALLY” /></div>

    Is there a way to grab the url dynamically, if the image name is limited to something like “postname-image”?

    If so, is there a way this could work for more than one image uploaded with a post — “postname-image1”, “postname-image2”?

Viewing 1 replies (of 1 total)
  • Yes, doable.

    $temp_meta = get_post_custom();
    $meta_img = $temp_meta["postname-image"][0];

    Then

    echo "<div class=\"genericphotostyle\" style=\"background-image:url({$meta_img})\"</div>";

    etc.

    To do multiple images, you’d obviously need multiple divs. You might want to run a PHP loop
    if (!empty($temp_meta["postname-image2"][0])) { echo "<div></div>"; }

Viewing 1 replies (of 1 total)
  • The topic ‘code to pull uploaded photo into css background’ is closed to new replies.