I write this code for show thumbnails from related posts. If related post has no thumbnail, it should show first pict from his content:
$output .= "<a href="".get_permalink($related_post->ID)."">";
if(get_the_post_thumbnail($related_post->ID)) {
$output .= get_the_post_thumbnail($related_post->ID, $options['thumbnail_size']);
} else {
$output .= catch_image( $related_post->ID );
}
$output .= "</a>";
and in function.php I have this function:
function catch_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)) {
$first_img = "url for noimage.jpg";
}
return $first_img;
}
But this give me just first image from This post, not from related post. How can I take content which I need?
Try with this function: