Last active
December 28, 2015 02:48
-
-
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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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">'); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Havent had any luck with this any idea where im going wrong? Grabs the image but not the video.