两招提取WordPress文章缩略图

针对WordPress文章缩略图的提取教程,网上已经有很多,办法是很多的,本文只向大家推荐两招,希望对大家有用。

方法一:直接用函数抓取日志内容里的第一个图片。

在主题函数文件functions.php里添加代码:

function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/
/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "在这里指定如果没有图片则显示的默认图片路径";
}
return $first_img;
}

在模板的主循环里,在你需要显示图片的位置中添加如下代码:

这样,就会自动调用内容里的第一个图片,如果没有图片,就自动显示指定的图片。

方法二:调用日志缩略图,并使用timthumb.php文件对图片大小进行限定。

首先下载最新的timthumb.php文件,保存到主题目录;

在主题函数文件functions.php文件里添加以下代码,以实现对内容里图片的提取:

/*开启日志缩略图*/
add_theme_support( 'post-thumbnails' );
function post_thumbnail( $width = 642,$height = 340 ){
    global $post;
    if( has_post_thumbnail() ){    //如果有缩略图,则显示缩略图
        $timthumb_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full');
        $post_timthumb = ''.$post->post_title.'';
        echo $post_timthumb;
    } else {
        $post_timthumb = '';
        ob_start();
        ob_end_clean();
        $output = preg_match('//i', $post->post_content, $index_matches);    //获取日志中第一张图片
        $first_img_src = $index_matches [1];    //获取该图片 src
        if( !empty($first_img_src) ){    //如果日志中有图片
            $path_parts = pathinfo($first_img_src);    //获取图片 src 信息
            $first_img_name = $path_parts["basename"];    //获取图片名
            $first_img_pic = get_bloginfo('wpurl'). '/cache/'.$first_img_name;    //文件所在地址
            $first_img_file = ABSPATH. 'cache/'.$first_img_name;    //保存地址
            $expired = 604800;    //过期时间
            if ( !is_file($first_img_file) || (time() - filemtime($first_img_file)) > $expired ){
                copy($first_img_src, $first_img_file);    //远程获取图片保存于本地
                $post_timthumb = ''.$post->post_title.'';    //保存时用原图显示
            }
            $post_timthumb = ''.$post->post_title.'';
        } else {    //如果日志中没有图片,则显示默认
            $post_timthumb = ''.$post->post_title.'';
        }
        echo $post_timthumb;
    }
}

代码中$width = 642,$height = 340分别用来指定图片的宽度和高度,不管你的图片是大是小,最后都将以这个大小输出。

在模板中要显示图片的位置添加代码来显示图片:

这样就实现了指定大小图片的输出,对原来的图片自动裁切。

思章老师

认准了方向,就要勇敢地走下去,十年磨一剑,我相信,只要坚持,一切都有可能。

相关日志

  1. 没有图片

    2009.10.07

    网站SEO关键词密度分析工具

    Image via Wikipedia 其实…

  2. 没有图片

    2010.03.25

    Adobe CS5功能介绍及相关视频

    Adobe在之前已经公布了很多cs5软件的新…

  3. 没有图片

    2009.07.09

    国内PHP开源建站程序排序与分析

    在国外优秀开源PHP建站系统对比分析一文我向…

  4. 没有图片

    2009.10.21

    菠萝筐升级到 WordPress 2.8.5

    今晚应该说是双升级,用了一下午的时间把我的系…

  5. 没有图片

    2010.04.28

    Supesite 7.5 自定义信息无法删除的终极解决办法

    用Supesite 7.5 做站,肯定会用到…

  6. 没有图片

    2011.04.06

    博客升级到 WordPress 3.1.1

    昨天还在WordPress官方关注WordP…

评论

  1. codee 2013.07.09 3:01下午

    wordpress默认有缩略图么?

  2. 花皙蔻 2011.10.11 10:09上午

    方法不错!!!!!!!!!

  3. 手扑网 2011.07.07 11:46下午

    浏览你网站时出现以下错误Warning: copy([/) [function.copy]: failed to open stream: No such file or directory in /home/lawsky/domains/lisizhang.com/public_html/wp-content/themes/CoolWorld/functions.php on line 56