ThaiSEOBoard.com

พัฒนาเว็บไซต์ => Programming => ข้อความที่เริ่มโดย: wasantec ที่ 12 พฤศจิกายน 2012, 20:38:07



หัวข้อ: สอบถามหน่อยครับดึงรูป wordpress
เริ่มหัวข้อโดย: wasantec ที่ 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;

ขอบคุณมากครับ


หัวข้อ: Re: สอบถามหน่อยครับดึงรูป wordpress
เริ่มหัวข้อโดย: Unlocker ที่ 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