Catch First Image
-
I want to modify this code for “catch_first_image” to include the styles in the Functions.php, as opposed to on the page surrounding the php.
<?php function catch_first_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 = null;
}
return $first_img;
}?>I currently use this on the page “<?php echo get_second_image();?>” surrounded by <img src=””/> tags, but I’m trying to create thumbnails and I want the function to say something like, “if there’s an image post it with a width of 100 px, but if there’s not an image default image = null.”
So I guess the css needs to go at the end for “return $first_img.”
I know it’s possible, but I can’t figure out the grammer for the PHP. Thanks!
- The topic ‘Catch First Image’ is closed to new replies.