本文實例講述了Wordpress自動提取內容中第一張圖片作縮略圖的方法。分享給大家供大家參考。具體分析如下:
有時我們在發布文章時沒有縮略圖,我們希望系統自動提取我們發布的文章內容中第一張圖片作為縮略圖了,下面我來給大家介紹Wordpress自動提取內容中第一張圖片作縮略圖方法,有需要了解的同學可參考.
在php中我們可以使用preg_match與preg_match_all來獲取內容中所有圖片地址.
第一步:在主題模板的function.php里添加下面的函數,代碼如下:
復制代碼
代碼如下://截取內容中第一張圖片函數
function catch_that_image() {
global $post, $posts;
ob_start();
ob_end_clean();
preg_match('/<imgs[^<>]*?src=['"]([^'"<>]+?)['"][^<>]*?>/i', $post->post_content, $matche);
if($matche[1])
return $matche[1];
//否則取默認圖片
return 'default.gif';
}
function catch_that_image() {
global $post, $posts;
ob_start();
ob_end_clean();
preg_match('/<imgs[^<>]*?src=['"]([^'"<>]+?)['"][^<>]*?>/i', $post->post_content, $matche);
if($matche[1])
return $matche[1];
//否則取默認圖片
return 'default.gif';
}
第二步:在wordpress需要調用縮略圖的地方直接用這個函數調用,如下面例子,代碼如下:
復制代碼
代碼如下:<img src="<?php echo catch_that_image(); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" width="200px" height="200px" />
希望本文所述對大家的WordPress建站有所幫助。
【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。