为了增加用户体验和阅读深度,很多朋友喜欢为自己的博客添加相关文章功能,有些是纯文字的,而有些则是带缩略图的,图文并茂可以让用户更加有点击欲望,今天这里简单说下带缩略图的相关文章的实现方法。
将以下代码加入single.php文件中想要显示带缩略图的相关文章处:
- <div id=”related-posts”>
- <div class=”caption”>带缩略图的相关文章:</div>
- <div class=”related” class=”clearfix”>
- <ul>
- <?php
- $post_num = 5;
- $exclude_id = $post->ID;
- $posttags = get_the_tags(); $i = 0;
- if ( $posttags ) {
- $tags = ”; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ‘,’;
- $args = array(
- ‘post_status’ => ‘publish’,
- ‘tag__in’ => explode(‘,’, $tags),
- ‘post__not_in’ => explode(‘,’, $exclude_id),
- ‘caller_get_posts’ => 1,
- ‘orderby’ => ‘comment_date’,
- ‘posts_per_page’ => $post_num,
- );
- query_posts($args);
- while( have_posts() ) { the_post();
- ?>
- <li><a href=”<?php echo the_permalink(); ?>” title=”<?php the_title(); ?>” ><img src=”<?php if ( get_post_meta($post->ID, ‘thumbnail1’, true) ) { echo get_post_meta($post->ID, ‘thumbnail1’, true);}
- elseif ( has_post_thumbnail() ){$thumbnail1_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), ‘thumbnail1’);echo $thumbnail1_url[0];}else{echo catch_first_image();} ?>” alt=”<?php the_title(); ?>” width=”120″ height=”120″ ></a>
- <a href=”<?php echo the_permalink(); ?>” ><?php the_title(); ?></a></li>
- <?php
- $exclude_id .= ‘,’ . $post->ID; $i ++;
- } wp_reset_query();
- }
- if ( $i < $post_num ) {
- $cats = ”; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ‘,’;
- $args = array(
- ‘category__in’ => explode(‘,’, $cats),
- ‘post__not_in’ => explode(‘,’, $exclude_id),
- ‘caller_get_posts’ => 1,
- ‘orderby’ => ‘comment_date’,
- ‘posts_per_page’ => $post_num – $i,
- );
- query_posts($args);
- while( have_posts() ) { the_post();
- ?>
- <li><a href=”<?php echo the_permalink(); ?>” title=”<?php the_title(); ?>” ><img src=”<?php if ( get_post_meta($post->ID, ‘thumbnail1’, true) ) { echo get_post_meta($post->ID, ‘thumbnail1’, true);}
- elseif ( has_post_thumbnail() ){$thumbnail1_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), ‘thumbnail1’);echo $thumbnail1_url[0];}else{echo catch_first_image();} ?>” alt=”<?php the_title(); ?>” width=”120″ height=”120″ ></a>
- <a href=”<?php echo the_permalink(); ?>” ><?php the_title(); ?></a></li>
- <?php
- $i++;
- } wp_reset_query();
- }
- if ( $i == 0 ) echo ‘<li>没有相关文章!</li>’;
- ?>
- </ul>
- </div>
- </div>
- <!– release post end –>
根据自己博客样式修改一下CSS代码:
-
- #related-posts{border-radius:5px;border-top:3px #dfdfdf solid;background:#fff;clear:both;}
- .caption{color:#0C0C0B;font-size: 14px;font-weight:bold;padding-top: 10px;margin-bottom: 10px;margin-left:10px;}
- .related {white-space:normal;margin:0 auto;overflow: hidden;border-left: 1px solid #DFDFDF;margin-left:20px;}
- .related li{float: left;width: 120px;height:180px;list-style:none;border-right:1px solid #dfdfdf;padding:4px;_padding:2px;text-align: left;}
- .related a{float: left;width: 126px;text-overflow: ellipsis;-o-text-overflow: ellipsis;}
- .related a:hover{white-space: normal;}
- .related li:hover{background:#F6F6F6;}
- .related img{height:120px;width:120px;border:0;padding:0;}
「两年博客,如果觉得我的文章对您有用,请帮助本站成长」
共有 0 条评论 - 给WordPress相关文章添加显示缩略图