两招提取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.02.05

    网站弹出登陆窗口设计

    大家都知道,很多博客或网站都有一个点击弹出登…

  2. 没有图片

    2010.04.20

    DedeCMS 5.6 正式版发布

    今天午饭后,闲着没事,就打开DedeCMS官…

  3. 没有图片

    2013.10.30

    WordPress模板开发获取优酷视频缩略图

    有时写WEB程序时本文主要介绍WordPre…

  4. 没有图片

    2014.12.17

    WordPress博客提速新选择:WP Acceleration for China

    写博客的朋友都在经历着一个艰难而漫长的体验:…

  5. 没有图片

    2015.05.30

    WordPress短代码实现移动设备上内容不可见

    许多人也都开始对自己的站点进行了移动设备适配…

  6. 没有图片

    2010.10.09

    搜索图片,你用哪个引擎?

    本文是针对今天菠萝使用百度、谷歌和必应搜索图…

评论

  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