本文將教會你如何為WordPress添加相關(guān)文章功能,并提供了標(biāo)題列表樣式和縮略圖樣式。相關(guān)文章的獲取思路是:Tags標(biāo)簽相關(guān)>同分類下文章,也就是說,先獲取標(biāo)簽相同的文章,如果還達(dá)不到數(shù)量,就調(diào)用該分類下的文章補(bǔ)足。獲取方法貌似最初來自Willin Kan 大師,倡萌再次修改。
1.添加標(biāo)題列表樣式的相關(guān)文章

將下面的代碼添加到 single.php 要顯示相關(guān)文章的位置即可:
<h3>相關(guān)文章</h3>
<ul class="related_posts">
<?php
$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="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?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 rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
<?php $i++;
} wp_reset_query();
}
if ( $i == 0 ) echo '<li>沒有相關(guān)文章!</li>';
?>
</ul>
PS:第四行$post_num = 8;表示顯示8篇文章,請根據(jù)自己的需要修改。
顯示樣式需要自己寫css,可以參考一下下面的:
.related_posts{margin-top:5px;}
.related_posts li{margin-left:20px;color:#444;list-style:circle;font-size:14px;line-height:26px;padding:0 0 0 5px}
2.添加含縮略圖的相關(guān)文章

倡萌只是根據(jù)上面的代碼改了一下,添加了縮略圖。
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('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$post_thumbnail_src = $matches [1] [0]; //獲取該圖片 src
if(empty($post_thumbnail_src)){ //如果日志中沒有圖片,則顯示隨機(jī)圖片
$random = mt_rand(1, 10);
echo get_bloginfo('template_url');
echo '/images/pic/'.$random.'.jpg';
//如果日志中沒有圖片,則顯示默認(rèn)圖片
//echo '/images/default_thumb.jpg';
}
};
echo $post_thumbnail_src;
}
PS:上面的代碼主要是獲取圖片鏈接,獲取的順序是:
自定義字段為 thumb 的圖片>特色縮略圖>文章第一張圖片>隨機(jī)圖片/默認(rèn)圖片;
隨機(jī)圖片:請制作10張圖片,放在現(xiàn)用主題文件夾下的 images/pic/ 目錄,圖片為jpg格式,并且使用數(shù)字 1-10命名,比如 1.jpg;如果你不想用隨機(jī)圖片,請將 倒數(shù)第5行 前面的“//”去掉,然后給 倒數(shù)第7、9行 前面添加“//”注銷,并且在現(xiàn)用主題的 /images/ 目錄下添加一張名字為 default_thumb.jpg 的默認(rèn)圖片,這樣,就會顯示默認(rèn)圖片。
2)將下面的代碼添加到 single.php 要顯示相關(guān)文章的位置:
<h3>相關(guān)文章</h3>
<ul class="related_img">
<?php
$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="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank">
<img src="<?php echo post_thumbnail_src(); ?>" alt="<?php the_title(); ?>" class="thumbnail" />
</a>
</div>
<div class="r_title"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank" rel="bookmark"><?php the_title(); ?></a></div>
</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 class="related_box" >
<div class="r_pic">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank">
<img src="<?php echo post_thumbnail_src(); ?>" alt="<?php the_title(); ?>" class="thumbnail" />
</a>
</div>
<div class="r_title"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank" rel="bookmark"><?php the_title(); ?></a></div>
</li>
<?php $i++;
} wp_reset_query();
}
if ( $i == 0 ) echo '<div class="r_title">沒有相關(guān)文章!</div>';
?>
</ul>
PS:第四行$post_num = 4; 表示調(diào)用4篇文章,請根據(jù)自己需要修改。
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}





麻煩能不能改一個 先獲取同分類下文章,同分類文章不夠的再獲取tags相同文章,我是個三把刀不知道怎么替換
你好,我想請問下,怎么隨機(jī)顯示同分類或者tag下的相關(guān)文章,按照上面代碼加上后設(shè)置4個相關(guān)文章,但是相關(guān)文章顯示的是相鄰的4篇文章,謝謝
我把’orderby’ => ‘comment_date’,改成’orderby’ => ‘rand’,也不起作用啊
錯了,不是相鄰的文章,是最新的4篇文章
代碼默認(rèn)就是調(diào)用相同標(biāo)簽的文章,確保你的確為文章添加標(biāo)簽
把’orderby’ => ‘comment_date’,改成’orderby’ => ‘rand’,已經(jīng)能隨機(jī)調(diào)文章了,剛沒清緩存,另外請教下,我使用的是dux1.8主題,文章設(shè)置了外鏈縮略圖,但是隨機(jī)顯示相關(guān)文章的時候,圖片調(diào)用的是文章里的第一張圖片,我想顯示成設(shè)置的外鏈縮略圖怎么修改?上面教程的獲取圖片鏈接順序是:
自定義字段為 thumb 的圖片>特色縮略圖>文章第一張圖片>隨機(jī)圖片/默認(rèn)圖片;
你好,不熟悉你說的這個主題,請聯(lián)系主題作者咨詢,謝謝
dux里面functions-theme.php是這些
add_theme_support( ‘post-formats’, array( ‘aside’ ) );
// post thumbnail
if (function_exists(‘add_theme_support’)) {
add_theme_support(‘post-thumbnails’);
set_post_thumbnail_size(220, 150, true );
}
和下面這段,我看不懂
function _get_post_thumbnail($size = ‘thumbnail’, $class = ‘thumb’) {
global $post;
$thumb_default = get_stylesheet_directory_uri() . ‘/img/thumbnail.png’;
$issrc = _hui(‘thumbnail_src’);
$html = ”;
if (has_post_thumbnail()) {
/*$domsxe = simplexml_load_string(get_the_post_thumbnail());
$src = $domsxe->attributes()->src;
$src_array = wp_get_attachment_image_src(_get_attachment_id_from_src($src), $size);
$html = sprintf(”, $src_array[0], $class);*/
// print_r($domsxe);
$domsxe = get_the_post_thumbnail();
preg_match_all(‘//sim’, $domsxe, $strResult, PREG_PATTERN_ORDER);
$images = $strResult[1];
foreach($images as $src){
if( $issrc ){
$html = sprintf(”, $thumb_default, $src);
}else{
$html = sprintf(”, $src, $post->post_title._get_delimiter().get_bloginfo(‘name’));
}
break;
}
} else {
$_de = ‘ data-thumb=”default”‘;
if( _hui(‘thumblink_s’) ){
$thumblink = get_post_meta($post->ID, ‘thumblink’, true);
if( !empty($thumblink) ){
$thumb_default = $thumblink;
$_de = ”;
}
}
if( $issrc ){
$html = sprintf(”, $thumb_default, $class);
}else{
$html = sprintf(”, $thumb_default, $class, $post->post_title._get_delimiter().get_bloginfo(‘name’));
}
}
return $html;
}
DUX6.4輸出相關(guān)文章縮略圖的代碼是
if( $thumb_s ) echo ''._get_post_thumbnail('full').'';輸出的縮略圖大大小小不整齊,請問有什么辦法調(diào)整輸出縮略圖的大小呢?錯了是 if( $thumb_s ) echo ‘‘._get_post_thumbnail(‘full’).’‘;
總算打出來了 是 if( $thumb_s ) echo '<a href="'.get_permalink().'">'._get_post_thumbnail('full').'</a>';
有可以通過文章標(biāo)題匹配相關(guān)文章的代碼嗎
沒有這方面的,還是建議你通過標(biāo)簽來獲取相關(guān)文章吧
那得搞數(shù)據(jù)分析了(喪)
我想把這段代碼放在首頁,做成cms豆腐塊的樣子。
求回復(fù)!!
要修改主題,自己學(xué)習(xí)下基本的知識吧:http://www.ydqwiac.cn/series/mastering-wp_query/
謝謝。
萌哥!可不可以指定調(diào)用某一分類ID下的文章。
比如我想要這段代碼指定調(diào)用分類ID:1 下面的文章。這樣可以實(shí)現(xiàn)嗎?
可以,百度一下就有方法
大神,css放在哪個文件里面。
一般可以添加到主題根目錄下的 style.css 文件的末尾
page頁面的相關(guān)文章、相關(guān)頁面調(diào)用,怎么樣弄精準(zhǔn)
function post_thumbnail_src(){ global $post; $post_thumbnail_src = ''; ob_start(); ob_end_clean(); $output = preg_match_all('//i', $post->post_content, $matches); $post_thumbnail_src = $matches [1] [0]; //獲取該圖片 src echo $post_thumbnail_src; }這是輸出文章第一張圖的的代碼吧 有問題啊 我文章中好幾張圖片 有的輸出的不是第一張 ?
研究了下 發(fā)現(xiàn)是第一張圖片不是指的文章中的第一張,而是多張圖在上傳的時候第一個傳好的圖片被定義為了第一張 ? ? 沒辦法 設(shè)特色圖當(dāng)封面吧
發(fā)現(xiàn)如果你文章中的圖片尺寸很大的話,獲得的特色圖片的尺寸就很大,會嚴(yán)重影響加載速度,百度了下獲取特色圖片的函數(shù)wp_get_attachment_image_src(),拿倡萌的那段代碼來說$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),’full’);看最后,是full,尺寸最大的,有點(diǎn)坑爹哦,倡萌應(yīng)該備注下,其中full可以修改的,可以為thumbnail, medium, large or full(分別代表最小的縮略圖、中等、大和原始尺寸)