两招提取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. 没有图片

    2010.09.26

    Firefox 4 简体中文版

    Mozilla Firefox是一个自由的,…

  2. 没有图片

    2010.03.03

    读取RSS Feed PHP代码

    为了实现网站页面里读取RSS Feed内容并…

  3. 没有图片

    2011.04.04

    给你的页面添加返回顶部按钮

    大家应该都经历过,优秀的网页都会给人一个良好…

  4. 没有图片

    2009.12.07

    在线相册程序Imagevue安装与使用教程

    上次给大家推荐了一款效果非常牛的在线相册PH…

  5. 没有图片

    2009.07.26

    几款免费的优秀的图片存放和分享的网站

    大家都知有名的图片存放和分享网站 Picas…

  6. 没有图片

    2011.10.08

    如何在你的网站添加Google+按钮

    一般写篇技术文都是长篇大论,生怕哪里写得不够…

评论

  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