Skip to content

Instantly share code, notes, and snippets.

@sicksand
Created March 5, 2014 14:20
Show Gist options
  • Save sicksand/9368055 to your computer and use it in GitHub Desktop.
Save sicksand/9368055 to your computer and use it in GitHub Desktop.
<?php
/* Display the first image of wordpress post.
Put these function on file : functions.php
usage <?php echo _get_first_image(); ?>
*/
function _get_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="";
if(isset($matches[1][0]))
$first_img = $matches[1][0];
return $first_img;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment