主要代码如下,只会输出含有图片的文章中的第一张图片,如果文章中没有图片,则不输出。此功能主要适合那些爱臭美却又嫌麻烦的博主。
| 01 02 03 04 05 06 07 08 09 10 11 12 13 |
<?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; ?> |
