ยินดีต้อนรับคุณ, บุคคลทั่วไป กรุณา เข้าสู่ระบบ หรือ ลงทะเบียน

เข้าสู่ระบบด้วยชื่อผู้ใช้ รหัสผ่าน และระยะเวลาในเซสชั่น

ThaiSEOBoard.comพัฒนาเว็บไซต์Programmingสอบถามหน่อยครับดึงรูป wordpress
หน้า: [1]   ลงล่าง
พิมพ์
ผู้เขียน หัวข้อ: สอบถามหน่อยครับดึงรูป wordpress  (อ่าน 638 ครั้ง)
0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้
wasantec
หัวหน้าแก๊งเสียว
*

พลังน้ำใจ: 586
ออฟไลน์ ออฟไลน์

กระทู้: 2,613



ดูรายละเอียด
« เมื่อ: 12 พฤศจิกายน 2012, 20:38:07 »

มีใครพอจะ แปลออกไหมครับว่ามันทำงานอย่างไร หรือ หลักการอย่างไร

code 1

function postimage($width,$height) {
    $scriptpath = get_bloginfo('template_directory');
    $attachments = get_children(array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order'));
   $img = array_shift($attachments);
   $imagelink = wp_get_attachment_image_src($img->ID,'full');
   $image = $imagelink[0];
   if($image=='') {
      $image = $scriptpath."/images/default.gif";
      echo '<img src="'.$scriptpath.'/scripts/timthumb.php?src='.$image.'&w='.$width.'&h='.$height.'&zc=1" alt="" />';
   }else {
        echo '<img src="'.$scriptpath.'/scripts/timthumb.php?src='.$image.'&w='.$width.'&h='.$height.'&zc=1" alt="" />';   
      
}
}

/* --------------------------------------------- */
code 2
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);
  $first_img = $matches [1]
  • ;

  if(empty($first_img)){
     $img_dir = get_bloginfo('template_directory');
    $first_img = $img_dir . '/images/thumb.gif';
  }
  return $first_img;

ขอบคุณมากครับ
บันทึกการเข้า
Unlocker
คนรักเสียว
*

พลังน้ำใจ: 44
ออฟไลน์ ออฟไลน์

กระทู้: 193



ดูรายละเอียด เว็บไซต์
« ตอบ #1 เมื่อ: 15 พฤศจิกายน 2012, 11:05:38 »

ฟังชั่นแรก postimage($width,$height)

อันนี้ทำงานร่วมกับฟังชั่นอื่นๆของ wordpress โดยจะดีงข้อมูลไฟล์แนบที่ตรงกับหมายเลขโพสต์ที่ต้องการออกมาแสดง กรณีที่ไม่มีรูปภาพ แทนที่ด้วย /images/default.gif แล้ว echo แสดงผลออกมา มีการย่อรูปด้วย timthumb ด้วย (อย่างน้อยต้องมีสคริปต์ timthumb ด้วยถึงจะเวิร์ก)

ฟังชั่นที่สอง get_post_image()

ฟังชั่นนี้ออกแบบมาค่อนข้างครอบคลุมดีครับ นำไปใช้ได้กับหลายๆสคริปต์ รวมทั้ง SMF ได้ด้วย
การทำงานก็คือ จะไปตรวจสอบ หาลิ้งค์รูปภาพจาก tag <img> ในโพสต์ ($post->post_content)
กรณีที่ไม่พบ ให้แทนค่าด้วย /images/thumb.gif แล้วรีเทิร์นค่ากลับไปทางตัวแปร $first_img

« แก้ไขครั้งสุดท้าย: 15 พฤศจิกายน 2012, 11:10:52 โดย Unlocker » บันทึกการเข้า

หน้า: [1]   ขึ้นบน
พิมพ์