wordpress主题制作、wordpress插件开发、WordPress优化
当前位置:首页 > wordpress优化 > 正文

wordpress抓取文章第一张图片为缩略图函数


用代码实现wordpress抓取文章第一张图片为缩略图函数,把下面的代码复制到主题文件夹下的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)){

$first_img = bloginfo('template_url'). '/screenshot.png';

}

return $first_img;

}

//*************************************
在要显示缩略图的地方调用如下代码
<img src="<?php echo catch_that_image(); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>"  width="227px" height="200px" /></a>
原创文章如转载请注明:转载帕克实验室

已经有2 个评论,你也说点什么吧

  1. hkmcgogo 说:

    正好用上,不过有插件好像可以解决

    [回复]
  2. 金一黄金 说:

    作者您好,改代码好象无效哦

    [回复]

发表评论

*

* 绝不会泄露



Design By 帕克实验室 | Power By Wordpress |网站地图|