I'm working on a website where i have setup to get the first and the second image from wordpress posts to use as custom homepage layout.
I'm using this code:
//GET FIRST IMAGE ON POST
function get_first_image($size = false) {
global $post, $_wp_additional_image_sizes;
$first_img = '';
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches[1][0];
if (empty($first_img)) {
return;
}
if ($size && $_wp_additional_image_sizes[$size]['crop'] == 1) {
$size = '-' . $_wp_additional_image_sizes[$size]['width'] . 'x' . $_wp_additional_image_sizes[$size]['height'] . '.jpg';
$pattern = '/-\d+x\d+\.jpg$/i';
$first_img = preg_replace($pattern, $size, $first_img);
}
return $first_img;
}
My problem is that my client use JPG for pictures in her blog, but there's some .GIF images in article, these .GIF i want to jump through and get only if the image is a valid .JPG image, how can i do this?
This regex will include jp(e)g and png, so will not include gif.
<img\s.*src='?".+(jpe?g|png).+>