user3550879
user3550879

Reputation: 3469

turn image into background image through php

I have the following code which wraps the last image in my wordpress post in a #last-img div.

html

<?php
    preg_match_all('/(<img [^>]*>)/', get_the_content(), $images);

    for( $i=0; isset($images[1]) && $i < count($images[1]); $i++ ) {
        if ($i == end(array_keys($images[1]))) {
            echo sprintf('<div id="last-img">%s</div>', $images[1][$i]);
            continue;
        }

        echo $images[1][$i];
    }
?>

css

#last-img { ... } 

I want the image to be the background of #last-img instead of just being inside it.

Upvotes: 0

Views: 102

Answers (1)

Itzik.B
Itzik.B

Reputation: 1076

I have change the pattern u chosed to use,

and instand of walk through on the entire array u can detect the last array in the var in the code below:

<?php
    preg_match_all('/src="([^"]*)"/i', get_the_content(), $images);
    echo $images[1][count($images[1])-1];
?>

You can use the "img src" as you please. GL.

Upvotes: 1

Related Questions