I am having a problem trying to get this snippet of code to output a separate og:image meta tag for each image in the array.
<?php function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){
//Defines a default image
$first_img = "http://example.com/images/fbthumb.jpg";
}
return $first_img;
}
?>
Currently the code is returning the first matched img src tag, but I would like it to return them all as separate tags so I can select which share image to use.
I know this line:
$first_img = $matches [1] [0];
needs to be changed into some type of for each condition but Im not sure how. Any help would be greatly appreciated. Thanks!
EDIT:
Here is my code after the last suggestion:
<?php function catch_that_image() {
global $post, $posts;
$result = preg_match_all('#<img.+src=[\'"]([^\'"]+)[^>]*>#i', $post->post_content, $matches);
return $matches[1];
}
?>
” />
I still cant figure it out. Any ideas?
Try this function. It will return an array of image sources. You could then always use the first image in the array as default image.
or simpler
Using the function: