wordpress中提取文章内第一张图片作为缩略图

作者:matrix 发布时间:2013 年 9 月 4 日 分类:Wordpress 兼容并蓄

网上有很多类似的代码,大多都来自知更鸟的HotNews。略有不同,这里还是记录一下。

一.模板函数中添加:

<?php //取文章内first_image  
function catch_first_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(emptyempty($first_img)){  
$random = mt_rand(1,10);  
echo get_bloginfo ( 'stylesheet_directory');  
echo '/images/random/'.$random.'.jpg';//若没有则输出/images/random/内图片  
}  
return $first_img;  
}  

?>  

二.主题目录内新建includes文件夹,thumbnail.php丢入: 阅读剩余部分 »