WordPress.org

Ready to get started?Download WordPress

Forums

code to pull uploaded photo into css background (2 posts)

  1. csleh
    Member
    Posted 6 years ago #

    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"?

  2. nudnik
    Member
    Posted 6 years ago #

    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>"; }

Topic Closed

This topic has been closed to new replies.

About this Topic