ループの中で、コンテンツに画像URIが含まれていれば、先頭の一枚画像を表示。
functios.php
/*
*
* $type empty or html
* $alt alt text when html
+ $width image width when html
**/
function my_get_content_image($type= '',$alt='',$width='auto'){
global $post;
if( ! preg_match("/(https?:\/\/)([-_.!*\'()a-zA-Z0-9;\/?:@&=+$,%#]+)\.(jpg |jpeg |gif |png)/iu",get_the_content(),$regs) ){
return false;
}
$url= $regs[1].$regs[2].'.'.$regs[3];
if( empty( $type ) ){
return $url;
}elseif( $type== 'html'){
$html= '<img src="%1$s" alt="%2$s" style="width:%3$spx;height:auto;max-width:100%;" />';
return sprintf( $html, esc_url( $url ), esc_attr( $alt ), esc_attr( $width ) );
}
}
テンプレートファイルのループの中に追加
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php echo get_content_image('html',get_the_title(), 100 );?>
<?php endwhile; ?>