來源于:
http://www.nafanlong.com/2473.html
一、標題/縮略圖樣式
1.添加標題列表樣式的相關文章

這種相關文章的獲取思路是:Tags標簽相關>同分類下文章,也就是說,先獲取標簽相同的文章,如果還達不到數量,就調用該分類下的文章補足。
將下面的代碼添加到 single.php 要顯示相關文章的位置即可:
<h3>相關文章h3>
<ul class="related_posts">
$post_num = 8;
$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 rel="bookmark" href="" title="" target="_blank"> the_title(); ?>a>li>
$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 rel="bookmark" href="" title="" target="_blank"> the_title(); ?>a>li>
$i++;
} wp_reset_query();
}
if ( $i == 0 ) echo '
沒有相關文章!
';
?>
ul>
PS:第四行$post_num = 8;表示顯示8篇文章,請根據自己的需要修改。顯示樣式需要自己寫css,可以參考一下下面的:
.related_posts{margin-top:5px;margin-left:10px;margin-bottom:20px;height:150px;list-style-position: inside;}
.related_posts li{margin-left:20px;color:#444;list-style:circle;font-size:14px;line-height:26px;padding:0 0 0 5px;float:left;width:45%;padding-right:9px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis}
2.添加含縮略圖的相關文章

1)在主題的 functions.php 的最后一個 ?> 前添加下面的代碼:
//添加特色縮略圖支持
if ( function_exists('add_theme_support') )add_theme_support('post-thumbnails');
//輸出縮略圖地址 From wpdaxue.com
function post_thumbnail_src(){
global $post;
if( $values = get_post_custom_values("thumb") ) { //輸出自定義域圖片地址
$values = get_post_custom_values("thumb");
$post_thumbnail_src = $values [0];
} elseif( has_post_thumbnail() ){ //如果有特色縮略圖,則輸出縮略圖地址
$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full');
$post_thumbnail_src = $thumbnail_src [0];
} else {
$post_thumbnail_src = '';
ob_start();
ob_end_clean();
$output = preg_match_all('//i', $post->post_content, $matches);
$post_thumbnail_src = $matches [1] [0]; //獲取該圖片 src
if(empty($post_thumbnail_src)){ //如果日志中沒有圖片,則顯示隨機圖片
$random = mt_rand(1, 10);
echo get_bloginfo('template_url');
echo '/images/pic/'.$random.'.jpg';
//如果日志中沒有圖片,則顯示默認圖片
//echo '/images/default_thumb.jpg';
}
};
echo $post_thumbnail_src;
}
PS:上面的代碼主要是獲取圖片鏈接,獲取的順序是:
自定義字段為:thumb 的圖片>特色縮略圖>文章第一張圖片>隨機圖片/默認圖片;
隨機圖片:請制作10張圖片,放在現用主題文件夾下的 images/pic/ 目錄,圖片為jpg格式,并且使用數字 1-10命名,比如 1.jpg;
如果你不想用隨機圖片,請將 倒數第5行 前面的“//”去掉,然后給 倒數第7、9行 前面添加“//”注銷,并且在現用主題的 /images/ 目錄下添加一張名字為 default_thumb.jpg 的默認圖片,這樣,就會顯示默認圖片。
2)將下面的代碼添加到 single.php 要顯示相關文章的位置:
<h3>相關文章h3>
<ul class="related_img">
$post_num = 4;
$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 class="related_box" >
<div class="r_pic">
<a href="" title="" target="_blank">
<img src="" alt="" class="thumbnail" />
a>
div>
<div class="r_title"><a href="" title="" target="_blank" rel="bookmark"> the_title(); ?>a>div>
li>
$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 class="related_box" >
<div class="r_pic">
<a href="" title="" target="_blank">
<img src="" alt="" class="thumbnail" />
a>
div>
<div class="r_title"><a href="" title="" target="_blank" rel="bookmark"> the_title(); ?>a>div>
li>
$i++;
} wp_reset_query();
}
if ( $i == 0 ) echo '
沒有相關文章!
';
?>
ul>
PS:第四行$post_num = 4; 表示調用4篇文章,請根據自己需要修改。css樣式自己寫,也可參考一下:
.related_posts{margin-top:5px;}
.related_img{width:600px;height:210px;}
.related_box{float:left;overflow:hidden;margin-top:5px;width:148px;border-right:1px #eee solid}
.related_box:hover{background:#f9f9f9}
.related_box .r_title{width:auto;height:72px;font-weight:400;font-size:14px;margin:0 10px;overflow:hidden;}
.related_box .r_pic{margin:6px}
.related_box .r_pic img{width:130px;height:100px;border:1px solid #e1e1e1;background:#fff;padding:2px}
二、五種不同添加(僅標題)方法
1、標簽相關
首先獲取文章的所有標簽,接著獲取這些標簽下的 n 篇文章,那么這 n 篇文章就是與該文章相關的文章了。現在可以見到的WordPress相關文章插件都是使用的這個方法。下面是實現的代碼:
"tags_related">
global $post;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
foreach ($post_tags as $tag) {
// 獲取標簽列表
$tag_list[] .= $tag->term_id;
}
// 隨機獲取標簽列表中的一個標簽
$post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];
// 該方法使用 query_posts() 函數來調用相關文章,以下是參數列表
$args = array(
'tag__in' => array($post_tag),
'category__not_in' => array(NULL), // 不包括的分類ID'post__not_in' => array($post->ID),
'showposts' => 6, // 顯示相關文章數量'caller_get_posts' => 1
);
query_posts($args);
if (have_posts()) {
while (have_posts()) {
the_post(); update_post_caches($posts); ?><li>* <a href="" rel="bookmark" title=""> the_title(); ?>a>li>
}
}
else {
echo '
* 暫無相關文章
';
}
wp_reset_query();
}
else {
echo '
* 暫無相關文章
';
}
?>
ul>
使用說明:"不包括的分類ID" 指的是相關文章不顯示該分類下的文章,將同行的 NULL 改成文章分類的ID即可,多個ID就用半角逗號隔開。因為這里限制只顯示6篇相關文章,所以不管給 query_posts() 的參數 tag__in 賦多少個值,都是只顯示一個標簽下的 6 篇文章,除非第一個標簽有1篇,第二個標簽有2篇,第三個有3篇。。。。。。所以如果這篇文章有多個標簽,那么我們采取的做法是隨機獲取一個標簽的id,賦值給 tag__in 這個參數,獲取該標簽下的6篇文章。
2、分類相關
本方法是通過獲取該文章的分類id,然后獲取該分類下的文章,來達到獲取相關文章的目的。
"cat_related">
global $post;
$cats = wp_get_post_categories($post->ID);
if ($cats) {
$args = array(
'category__in' => array( $cats[0] ),
'post__not_in' => array( $post->ID ),
'showposts' => 6,
'caller_get_posts' => 1
);
query_posts($args);
if (have_posts()) {
while (have_posts()) {
the_post(); update_post_caches($posts); ?><li>* <a href="" rel="bookmark" title=""> the_title(); ?>a>li>
}
}
else {
echo '
* 暫無相關文章
';
}
wp_reset_query();
}
else {
echo '
* 暫無相關文章
';
}
?>
ul>
方法三:標簽相關,SQL獲取
獲取相關文章的原理與方法一相似,不過在獲取文章的時候是以SQL語句來直接讀取數據庫,從而隨機獲取6篇相關文章記錄,而不是WordPress的函數query_posts()。
"tags_related">
global $post, $wpdb;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
$tag_list = '';
foreach ($post_tags as $tag) {
// 獲取標簽列表
$tag_list .= $tag->term_id.',';
}
$tag_list = substr($tag_list, 0, strlen($tag_list)-1);
$related_posts = $wpdb->get_results("
SELECT DISTINCT ID, post_title
FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy
WHERE {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id
AND ID = object_id
AND taxonomy = 'post_tag'
AND post_status = 'publish'
AND post_type = 'post'
AND term_id IN (" . $tag_list . ")
AND ID != '" . $post->ID . "'
ORDER BY RAND()
LIMIT 6");
// 以上代碼中的 6 為限制只獲取6篇相關文章// 通過修改數字 6,可修改你想要的文章數量
if ( $related_posts ) {
foreach ($related_posts as $related_post) {
?><li><a href="ID); ?>" rel="bookmark" title="post_title; ?>"> echo $related_post->post_title; ?>a>li>
}
}
else {
echo '
暫無相關文章
';
}
}
else {
echo '
暫無相關文章
';
}
?>
ul>
方法四:分類相關,SQL獲取
獲取相關文章的原理與方法二相似,不過在獲取文章的時候是以SQL語句來直接讀取數據庫,從而隨機獲取6篇相關文章記錄,而不是WordPress的函數query_posts()。
"cat_related">
global $post, $wpdb;
$cats = wp_get_post_categories($post->ID);
if ($cats) {
$related = $wpdb->get_results("
SELECT post_title, ID
FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy
WHERE {$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id
AND {$wpdb->prefix}term_taxonomy.taxonomy = 'category'
AND {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id
AND {$wpdb->prefix}posts.post_status = 'publish'
AND {$wpdb->prefix}posts.post_type = 'post'
AND {$wpdb->prefix}term_taxonomy.term_id = '" . $cats[0] . "'
AND {$wpdb->prefix}posts.ID != '" . $post->ID . "'
ORDER BY RAND( )
LIMIT 6");
if ( $related ) {
foreach ($related as $related_post) {
?><li>* <a href="ID); ?>" rel="bookmark" title="post_title; ?>"> echo $related_post->post_title; ?>a>li>
}
}
else {
echo '
* 暫無相關文章
';
}
}
else {
echo '
* 暫無相關文章
';
}
?>
ul>
方法五:作者相關
該方法是獲取該文章作者的其他文章來充當相關文章,代碼如下:
"author_related">
global $post;
$post_author = get_the_author_meta( 'user_login' );
$args = array(
'author_name' => $post_author,
'post__not_in' => array($post->ID),
'showposts' => 6, // 顯示相關文章數量'orderby' => date, // 按時間排序'caller_get_posts' => 1
);
query_posts($args);
if (have_posts()) {
while (have_posts()) {
the_post(); update_post_caches($posts); ?><li>* <a href="" rel="bookmark" title=""> the_title(); ?>a>li>
}
}
else {
echo '
* 暫無相關文章
';
}
wp_reset_query();
?>
ul>