Wordpress ดึงรูปแรกในโพสมาเป็น thumb ทำยังไง

เริ่มโดย scripture, 16 กุมภาพันธ์ 2015, 16:43:21

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

scripture

Wordpress ดึงรูปแรกในโพสมาเป็น thumb ทำยังไงครับ
ไม่เอาวิธีปลั๊กอินนะครับ
ขอบคุณครับ :wanwan017:

gapgag55


viwatchai

อันนี้เอาไว้ใน functions นะครับ
// Get URL of first image in a post
function catch_that_image() {
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];

// no image found display default image instead
if(empty($first_img)){
$first_img = "/img/default.png";
}
return $first_img;
}


เวลาเรียกก็ประมาณนี้นะครับ
<img src="<?php echo catch_that_image() ?>" />

หนุ่มบ้านโคก

ผมแนะนำว่าเพิ่มใน custom field หรือ รูปพิเศษ ที่มีมาให้ Theme นั้นๆ นั่นแหละครับ ดีกว่า ในระยะยาวดึงภาพแรกมามันไม่เวิร์คน่ะ มันมีผลกับความเร็วในการโหลด
https://developers.google.com/speed/pagespeed/insights/

ความเห็นส่วนตัวน่ะครับ  :P