Skip to content

Instantly share code, notes, and snippets.

@torounit
Created August 2, 2013 01:13
Show Gist options
  • Save torounit/6136786 to your computer and use it in GitHub Desktop.
Save torounit/6136786 to your computer and use it in GitHub Desktop.
WordPressで、記事内の最初の画像を持ってくる
<?php
function the_content_image( $size = 'thumbnail', $align = "" ) {
global $post;
$output = preg_match('/<img.+class=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
if( $output ) {
$first = $matches;
preg_match("/wp-image-([0-9]+)/", $first[1], $matches);
$attr = "";
if( $align ) {
$attr = array( "class" => $align );
}
echo wp_get_attachment_image( $matches[1], $size , false , $attr);
} else {
//$output = preg_match('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
//echo $matches[0];
}
return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment