只要取第一張圖片的方法
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() 的值只有圖片網址,需自行加上 )
你或許會想要看:
- WordPress 4.2 發佈了,命名為 “Powell”
- 超推薦 WordPress 上最完整的 Google 服務外掛 [功能啟用與畫面配置]
- 超推薦 WordPress 上最完整的 Google 服務外掛 [介紹篇]
- [wordpress] 如何讓文章 (post) 也能套用不同的版型?
- 單一個wordpress網站使用多個網址
- [wordpress] 取的某分類頁面的term_id
- [教學] ckeditor 4.3 開啟上傳圖檔與瀏覽檔案功能
- [wordpress] 製作像 bootstrap 樣式的導航條、工具列
- [wordpress] register_taxonomy 自定新的分類
- [CodeIgniter] 取得 mysql 欄位資訊的編碼(Collation)、權限(Privileges)、註解(Comment)的欄位