functions.phpに以下記述
function catch_first_image($prefix='',$postfix='') {
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)){
return $prefix.$first_img.$postfix;
}
}
テンプレート内で呼び出す
<?php $wp_query = new WP_Query( array( 'post_type' => array( 'report','information' ), 'posts_per_page' => 5 ) ); ?>
<?php if (have_posts()) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<?php echo catch_first_image('<img src="','" width="280px">'); ?>
最終更新:2013年02月05日 21:36