Notice: Undefined offset
Posted: Sat Jun 30, 2012 3:24 pm
This is the code for a wordpress function to grab the first post image and return it.
Now this was to be used as Image like this:
Strangely I am getting a notice that reads:
Notice: Undefined offset: 0 in C:\wamp\www\wordpress\wp-content\themes\GM\functions.php on line 58 Call Stack #TimeMemoryFunctionLocation .
FYI: Line 58 in the above code is $first_img = $matches [1][0];
I googled all over the place but could not fix this.
Any suggestions ? And what exactly does undefined offset mean ?
Thanks
Code: Select all
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 = "/images/default.jpg";
}
return $first_img;
}
Code: Select all
<img width="120px" height="120px"src="<?php echo catch_that_image() ?>" >.Strangely I am getting a notice that reads:
Notice: Undefined offset: 0 in C:\wamp\www\wordpress\wp-content\themes\GM\functions.php on line 58 Call Stack #TimeMemoryFunctionLocation .
FYI: Line 58 in the above code is $first_img = $matches [1][0];
I googled all over the place but could not fix this.
Any suggestions ? And what exactly does undefined offset mean ?
Thanks