ลองดูวิธีที่ผมใช้
1.แทรก functionเรียกรูป ใน function.php
/*-----------------------------------------------------------------------------------*/
/* Function to call first uploaded image in functions file
/*-----------------------------------------------------------------------------------*/
function front_main_image() {
global $post, $posts;
//This is required to set to Null
$id='';
$the_title='';
// Till Here
$permalink = get_permalink( $id );
$homeLink = get_template_directory_uri();
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
if(isset($matches [1] [0])){
$first_img = $matches [1] [0];}
if(empty($first_img)){ //Defines a default image
$first_img = "$homeLink/images/default.jpg";
print "<a href='$permalink'><img src='$first_img' class='img-front wp-post-image' alt='$the_title' /></a>";
}
else{
print "<a href='$permalink'><img src='$first_img' class='img-front wp-post-image' alt='$the_title' /></a>";
}
}
/*-----------------------------------------------------------------------------------*/
/* Function to call first uploaded image in functions file
/*-----------------------------------------------------------------------------------*/
วางไว้ตรงไหนก็ได้ใน function.php
2.เพิ่มอันนี้ ใน index.php
<div class="content-first-img"><?php echo front_main_image(); ?></div>
วางไว้ก่อน แท็ก entry title และ the_excerpt
3.เพิมอันนี้ ใน style.css
.content-first-img {float:left; width:250px; padding:0px; margin:0px 10px 10px 0px;}
.content-first-img img.img-front {background-color:#fff; padding:4px; border:1px solid #ddd; width:240px;}
พวก width, margin, color ต่างๆ ปรับตามต้องการได้ครับ
ผมไม่ได้เขียน function เองนะ ก็ copy มาเหมือนกันเอามาใส่ใน theme ที่ถูกใจ
ลองดูครับเผื่อจะใช้ได้