Created
August 2, 2013 01:13
-
-
Save torounit/6136786 to your computer and use it in GitHub Desktop.
WordPressで、記事内の最初の画像を持ってくる
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 | |
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