在Function.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 = get_bloginfo('template_directory')."/images/default.jpg";
}
return $first_img;
}
调用代码(写出大致功能实现方法,具体样式请自己修改):
<?php
query_posts(cat=分类ID,showposts=调用数量);
?>
<ul>
<?php if (have_posts()) :while (have_posts()) : the_post(); ?>
<li>
<img src="<?php echo catch_that_image(); ?>" alt="<?php the_title(); ?>" />//缩略图
<a href="<?php the_permalink() ?>" ><?php the_title(); ?></a>//标题
/*文章摘要开始*/
<?php
if ($post->post_excerpt) {
echo $post->post_excerpt;
}
else{
echo cut_str(strip_tags(apply_filters('the_content', $post->post_content)),40,"…");
}
?>
/*文章摘要结束*/
</li>
<?php endwhile; endif; ?>
</ul>
<?php wp_reset_query(); ?>
追问请问你这些哪里该放到Function.php 里 哪些是调用啊