為了增加用户體驗和閲讀深度,很多朋友喜歡為自己的博客添加相關文章功能,有些是純文字的,而有些則是帶縮略圖的,今天這裏我就簡單説下帶縮略圖的相關文章的實現方法。

將以下代碼加入 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), // 只選 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 ) { // 如果 tags 文章數量不足, 再取 category 補足.
$cats = ”; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ‘,’;
$args = array(
‘category__in’ => explode(‘,’, $cats), // 只選 category 的文章.
‘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 post thumbnail **/

#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;}

也可以把上面第一步中的 php 函數代碼放在單獨一個文件中,如 likeposts.php,然後在想要調用的頁面添加調用代碼:

<?php include(‘includes/likeposts.php’);?>

以上!