แบบที่1
แสดงรูปแรก
และเป็นขนาดใหญ่สุด
ถ้าไม่เจอ จะแสดงรูป Default
// Get Standard Post Image
function get_post_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
if (!empty($matches[1][0]) ) {
$first_img = $matches[1][0];
} else {
/*$first_img = 'http://insurancethai.net/wp-content/themes/insurancecenter/images/logo.png';*/
$first_img = 'http://insurancethai.net/wp-content/themes/insurancecenter/images/random-img/rotate.php';
}
return $first_img;
}
<img src="<?php echo get_post_image(); ?>" alt="<?php the_title(); ?>"/>
เราต้องการให้แสดงภาพแรก ถ้าไม่เจอ แทนที่จะแสดงรูปที่ต้องการ ให้แสดง excerpt แทน
$first_img = '.............';
ใช้แทน
$first_img = 'http://insurancethai.net/wp-content/themes/insurancecenter/images/random-img/rotate.php';
แต่ต้องแก้ code ส่วนแสดง
จาก
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<img src="<?php echo get_post_image(); ?>" alt="<?php the_title(); ?>" width="200" height="100" /></a>
เป็น
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php echo get_post_image(); ?></a>
มันจะดึง ........... ที่เราได้กำหนดไว้ ใน functions.php มาแสดงในหน้า แสดงผล แทน
โดยหน้าแสดงผลจะเห็นว่า มี <img src... ซึ่งต้องเอาออกไป
เพราะ เป็น tag การแสดงภาพ โดยนำ url จาก code เดิมใน functions.php มาใส่
คือ จะเป็น <img src="รูป"> ซึ่งถ้าไม่เอาออก excerpt ก็ใช้ไม่ได้ เพราะ ไม่ใช่ url
แต่จะสร้างปัญหาใหม่
คือ ถ้าโพสนั้นมีรูป มันก็จะโชว์เป็น url ของรูป แทนรูป เพราะ tag หน้าแสดงผล มันไม่มี <img =scr.. นั่นเอง
จะทำอย่างไร?
เราก็เอา code ส่วนที่แสดง ที่ถูกตัดออกไป มาใส่ในค่าใน functions.php แทน (วิธีใส่ ดูด้านล่าง ***)
จะได้ไม่ต้องแสดงแต่ url เหมือน code เดิม
แต่จะแสดงเป็น code แสดงรูปสมบูรณ์ในตัว คือ มี tag img
<img src="<?php echo get_post_image(); ?>" alt="<?php the_title(); ?>"/>แบบที่2
แสดงรูป ขนาดของ WordPress 150×150
/*post_thumb ปรับจำนวนที่ setting>read ใน cp admin*/
function post_thumb() {
$files = get_children('post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image');
if($files) :
$keys = array_reverse(array_keys($files));
$j=0;
$num = $keys[$j];
$image=wp_get_attachment_image($num, 'thumbnail', false);
$imagepieces = explode('"', $image);
$imagepath = $imagepieces[1];
$thumb=wp_get_attachment_image($num, 'thumbnail', false);
print "$thumb";
endif;
}แสดงรูป ขนาดของ WordPress medium
เลือกแสดงรูป ลำดับไหนมาก็ได้
ตั้งค่าลำดับรูปที่ต้องการแสดงได้
ถ้าไม่กำหนดไปมันจะแสดงทุกรูป
ถ้าไม่มีรูปแสดงค่าว่าง (ไม่แสดงอะไรมา)
ใช้เพียง code ส่วนแสดงผล
<?php
$size = 'medium'; // whatever size you want
if ( has_post_thumbnail() ) {
the_post_thumbnail( $size );
} else {
$attachments = get_children( array(
'post_parent' => get_the_ID(),
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC', // ลำดับการดึงรูป จากบนลงล่าง
'orderby' => 'menu_order ID',
'numberposts' => 1) // โชว์ 1รูป
);
foreach ( $attachments as $thumb_id => $attachment ) {
echo wp_get_attachment_image($thumb_id, $size);
}
}
?>
ถ้าใส่แบบย่อ มันจะโชว์รูปทั้งหมดใน โพสนั้น แทนที่จะโชว์ รูปเดียว
<?php
$size = 'medium';
if ( has_post_thumbnail() ) {the_post_thumbnail( $size );}
else
{$attachments = get_children( array('post_parent' => get_the_ID(),));
foreach ( $attachments as $thumb_id => $attachment )
{echo wp_get_attachment_image($thumb_id, $size);}
}
?>เราจะเอาส่วนแสดงผลให้ไปอยู่ใน functions.php และเรียกมาใช้แค่ code สั้นๆก็ได้
function get_post_image-medium() {
$size = 'medium';
if ( has_post_thumbnail() ){
the_post_thumbnail( $size );
}
else{$attachments = get_children
(
array(
'post_parent' => get_the_ID(),
'order' => 'ASC',
'numberposts' => 1
)
);
foreach ( $attachments as $thumb_id => $attachment )
{echo wp_get_attachment_image($thumb_id, $size);
}
}
}
ส่วนแสดงผล จะเป็น code สั้นๆ แทน ส่วนแสดงผลอันเดิม
<?php get_post_image-medium() ?>
แบบที่3
แสดงรูปแรกแบบลดขนาดของ wordpress
ไม่มีรูป จะแสดงค่าว่าง (ไม่ดึงอะไรมา)
// เรียกรูปแรก แบบที่3
//function to call first uploaded image in functions file
function main_image() {
$files = get_children('post_parent='.get_the_ID().'&post_type=attachment
&post_mime_type=image&order=desc');
if($files) :
$keys = array_reverse(array_keys($files));
$j=0;
$num = $keys[$j];
$image=wp_get_attachment_image($num, 'large', true);
$imagepieces = explode('"', $image);
$imagepath = $imagepieces[1];
$main=wp_get_attachment_url($num);
$template=get_template_directory();
$the_title=get_the_title();
print "<img src='$main' alt='$the_title' class='frame' />";
endif;
}
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) {
echo get_the_post_thumbnail($post->ID);
} else {
echo main_image();
} ?>
ถ้าเอาการแสดงผลของ แบบ2 (medium)ไปแทน การแสดงผล ของแบบ1
จะทำให้
แสดงรูป ขนาดของ WordPress medium
เลือกแสดงรูป ลำดับไหนมาก็ได้
ตั้งค่าลำดับรูปที่ต้องการแสดงได้
ถ้าไม่กำหนดไปมันจะแสดงทุกรูป
ถ้าไม่มีรูปแสดงค่าว่าง (ไม่แสดงอะไรมา)
กว้างยาวของภาพ 300 (medium) จะถูกปรับโดย css ของ แบบ1
นั่นคือ ไม่ต้องดึงรูป ขนาดใหญ่มาแล้ว แถมไม่จำเป็นต้องแสดงออกมาแบบเต็ม300 เพราะ css คุมอยู่
***
code นี้ <?php $first_img = $matches[1][0]; ?> ค่า $first_img แสดงเป็น url รูป ----- ผมต้องการเอา <img src=" "> ใส่คร่อมเข้าไปได้อย่างไรครับ ลองแบบนี้แล้วมันเออร์เร่ออ่ะ <img src="$matches[1][0]"/> ----- เพิ่มเติม อยากให้ออกมาเป็นรุปนี้อ่ะ <?php $first_img = <img src="$matches[1][0]"/> ?> เพราะ ต้องการจะเอาค่า $first_img ไป ใช้อีกที
ตอบ
<?php $first_img = '<img src="'.$matches[1][0].'"/>'; ?>
https://web.facebook.com/groups/122558751110047/permalink/1896128987086339/?comment_id=1896137760418795&reply_comment_id=1896140970418474¬if_id=1524736397418615¬if_t=group_comment&ref=notif