WordPress 自動(dòng)獲取文章第一張圖片:
在當(dāng)前使用的主題模板的functions.php文件中添加以下代碼:
- function catch_that_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] [0];
- if(empty($first_img)){ //Defines a default image
- $first_img = "/images/default.jpg";
- }
- return $first_img;
- }
在當(dāng)前主題模板的index.php文件的內(nèi)容代碼前或后添加以下代碼:文章源自網(wǎng)吧系統(tǒng)維護(hù)-http://www.strong-digital.cn/7723.html
- <?php echo catch_that_image() ?>
版權(quán)聲明:文章圖片資源來源于網(wǎng)絡(luò),如有侵權(quán),請留言刪除!!!
評論