随身笔记
随身笔记

wordpress无插件实现调用文章第一张图片当做首页缩略图

在当前使用的主题模板的functions.php文件<?php和?>之前添加以下代码:

[code]

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;

}
在当前主题模板的index.php文件的内容代码前或后添加以下代码:
<img src=”<?php echo catch_that_image(); ?>” alt=”<?php the_title(); ?>” title=”<?php the_title(); ?>” width=”227px”
height=”200px” />[/code]

文章来源:个人随笔 香格里拉seo (www.xgllseo.com)转载请注明,请尊重版权。
没有标签
首页      前端资源      wordpress教程      wordpress无插件实现调用文章第一张图片当做首页缩略图

随身笔记

wordpress无插件实现调用文章第一张图片当做首页缩略图
在当前使用的主题模板的functions.php文件<?php和?>之前添加以下代码: [code] function catch_that_image() { global $post, $posts; $first_img = ''; ob_start();…
扫描二维码继续阅读
2013-01-13