只要取第一張圖片的方法

function get_firest_image(){
  ob_start();
  ob_end_clean();
  preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $post->post_content, $matches);
  return $matches[1];
}


取第一張圖片和特色圖片一起判斷,如果沒有特色圖片,就找第一張圖片替代。

function get_firest_image(){
  global $post, $posts;
  $first_img = '';
  if ( has_post_thumbnail() )
  {
    $first_img = wp_get_attachment_url( get_post_thumbnail_id() );
  }
  else
  {
    ob_start();
    ob_end_clean();
    $output = preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $post->post_content, $matches);
    $first_img = $matches[1];
  }
  return $first_img;
}

(補充:get_firest_image() 的值只有圖片網址,需自行加上

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *