人生の狭間にて

メモ・勉強・ビジネス・批評・子育てのことなどを書いています。

WPで投稿記事をTOPページへ表示する方法

WPで投稿記事をTOPページへ表示する方法

投稿リストにサムネイルを付けることも可能。

 

WordPress › フォーラム » 記事中一番最初の画像を自動表示するが画像なしの時はHTML非表示にしたい

 f:id:Massa:20140424123148j:plain

function.phpの記述

function catch_that_image() {
	global $post;

	if ( preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches) ) {
		$first_img = $matches[1][0];
	} else {
		$first_img = false;
	}
	return $first_img;
}

 

 

home.phpの記述

<?php if ( $first_image = catch_that_image() ) : ?>
<div class="box">
	<img src="<?php echo $first_image; ?>" width="120" alt="" class="imgR" />
</div>
<?php endif; ?>