Skip to content

Instantly share code, notes, and snippets.

@stephenscaff
Last active December 28, 2015 02:48
Show Gist options
  • Save stephenscaff/7430097 to your computer and use it in GitHub Desktop.
Save stephenscaff/7430097 to your computer and use it in GitHub Desktop.
Revision of the great catch_that_image Wordpress function (which grabs the first image of a post instead of a featured image) to grab the placeholder image from say a YouTube or Vimeo vid. For output, a little conditional says, "Got a first image? No? Well, got an iframe image? Naws? Well, then just use a default image. After many clients gumpin…
function catch_that_iframe() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<iframe.+src=[\'"]([^\'"]+)[\'"].*><\/iframe>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
return $first_img;
}
<?php
$topimg = catch_that_image(); // Find first image of Post
$topiframe = catch_that_iframe();
if ($topimg) {
echo('<img src="'.$topimg.'">');
} else if ($topiframe) {
echo('<iframe src="' . $topiframe . '"></iframe>');
} else {
echo('<img src="/images/default.jpg">');
}
?>
@badincite
Copy link

Havent had any luck with this any idea where im going wrong? Grabs the image but not the video.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment