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

    2011.04.15

    WordPress实时显示评论者头像

    在很多朋友的博客上看到评论框输入邮箱后,会自…

  2. 没有图片

    2012.12.12

    WordPress 3.5 正式版发布 李思章博客升级

    本来只打算更新一下WordPress插件技术…

  3. 没有图片

    2012.09.20

    Discuz!x2.5完整伪静态.htaccess代码,支持论坛、博客、门户伪静态

    前段时间因为玩DZ论坛,需要把博客伪静态,就…

  4. 没有图片

    2010.01.27

    DedeCMS 5.5 SP1 年底发布

    今天突然想起我曾经相处多年的DedeCMS了…

  5. 没有图片

    2013.04.27

    CentOS MySQL 用户及数据库管理手册

    一、root用户密码的维护: 由于安装MyS…

  6. 没有图片

    2010.09.27

    Access 2010 的功能和优点

    Microsoft Access 2010 …

评论

  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