如何修改wordpress的php带来来实现这个首页自动显示文章内容缩略图的功能。
1.找到主题下的functions.php,增加一个现实第一个图片的方法。我是用的是 twentyeleven 主题,所以修改文件存在于 wp-content/themes/twentyeleven/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'). '/images/default-thumb.jpg';
}
return $first_img;
}
2.首页是index.php生成的,我们能看到以下代码,
<a href="<?php%20the_permalink();%20?>"><img src="<?php%20echo%20catch_that_image()%20?>" alt="<?php the_title(); ?>"/></a> //增加这一行即可