Reputation: 3469
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
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