主要代码如下,只会输出含有图片的文章中的第一张图片,如果文章中没有图片,则不输出。
<?php $posts = get_posts( "category=($cat->term_id)&numberposts=7" ); ?> <?php if( $posts ) : ?> <?php foreach( $posts as $post ) : setup_postdata( $post ); ?> <!-- 只调用含有图片的文章 开始 --> <?php $isimages = 0; $content = $post->post_content; $searchimages = '~<img [^>]* />~'; preg_match_all( $searchimages, $content, $pics ); $iNumberOfPics = count($pics[0]); if ( $iNumberOfPics > 0 ) { $isimages = 1; } ?> <?php if ($isimages == 1) : ?> <li> <a class="pic" href="<?php the_permalink(); ?>"><img width="180" height="124" src="<?php post_thumbnail_src(); ?>"></a> <div class="txt"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div> </li> <?php endif;?> <!-- 只调用含有图片的文章 结束 --> <?php endforeach; ?> <?php endif; ?>