版权声明: 转载时请以超链接形式标明文章原始出处和作者信息
本文来自: Domety»《Wordpress中为你的文章添加“相关文章”》
本文链接: http://domety.com/archives/146/
本文作者: DDBug
发表时期: 2009-11-10
前几天向大家介绍了如何实现“随机文章”的方法,今天将要介绍的这个“相关文章”的方法其实是差不多的,在随机文章的基础上稍加修改即可。
首先我们看一下什么样的文章才算是“相关文章”,对“相关文章”这个词的理解,不同人的理解可能会有点出入。我是这样认为的:具有相同标签的文章相关性最强,然后是具有相同分类的分章。
根据上面我对“相关文章”的理解,查找“相关文章”的过程如下:
- 首先随机显示和文章具有相同标签的文章
- 如果找到的文章数量不足指定的数量,则继续随机显示和文章分类相同的文章
- 如果还是不足指定的文章数量,则随机在全站显示几篇文章
关键代码分析
由于随机显示的工作比较多,我们首先来定义一个可以根据分类ID或者标签名显示随机文章的函数the_rand_posts,有关the_rand_posts函数,可以参看WordPress添加“随机文章”模块 一文。下面是升级版本的the_rand_posts函数
function the_rand_posts($args = '',$post_not_in = array()){
global $post;
$default = array('showposts'=>10,
'cat'=>'',
'tag'=>'',
'class'=>'randomPosts',
'useul'=>1);
$r = wp_parse_args($args,$default);
extract($r);
$r['orderby'] = 'rand';
$r['post__not_in'] = $post_not_in;
$rand_query = new WP_Query($r);
$output = '';
$post_ids = array();
foreach($rand_query->posts as $p){
$post_ids[] = $p->ID;
$output .= '<li><a href="'.get_permalink($p).'">'.get_the_title($p->ID).'</a></li>';
}
if($useul)
$output = '<ul>' .$output . '</ul>';
echo $output;
return $post_ids;
}
其中showposts指定显示的文章数量,cat指定查找的分类ID范围,tag指定查找的标签名范围,useul决定是否使用<ul></ul>标签。该函数返回查找到的文章ID数组。
下面看一下如何根据文章标签查找指定数量的相关文章:
$terms = get_the_tags();
foreach($terms as $term){
$tags[] = $term->name;
}
这段代码的作用是获取文章的标签名,保存到$tags数组中
接下来把$tags数组转换为用’,’号隔开的字符串,并传递给the_rand_posts来随机显示
$tag = implode(",",$tags);
$post_not_in[] = $post->ID;
$post_ids = the_rand_posts("useul=0&showposts=$showposts&tag=$tag",$post_not_in);
如果知道查找到的文章的数量呢,通过计算$post_ids数组的长度即可。$post_not_in是用来避免查找复制的文章。如果文章数量不足我们指定的$showposts,则再根据文章分类ID继续查找
$post_not_in = array_merge($post_not_in,$post_ids);
$count = count($post_ids);
if($count < $showposts){
$showposts -= $count;
$categorys = get_the_category();
foreach($categorys as $category){
$cats[] = $category->term_id;
}
$cat = implode(",",$cats);
$post_ids = the_rand_posts("useul=0&showposts=$showposts&cat=$cat",$post_not_in);
}
该段代码中通过调用get_the_category函数来获取文章的分类信息,并通过一个循环来保存分类ID列表,然后传递给the_rand_posts函数进行查找并随机显示。
最后我们再做一次判断,如果显示的文章数量仍然不足我们指定的文章数量,就从全站的文章中随机显示几篇
$count = count($post_ids);
$post_not_in = array_merge($post_not_in,$post_ids);
if($count < $showposts){
$showposts -= $count;
the_rand_posts("useul=0&showposts=$showposts",$post_not_in);
}
使用方法
完整代码如下:
function the_rand_posts($args = '',$post_not_in = array()){
global $post;
$default = array('showposts'=>10,
'cat'=>'',
'tag'=>'',
'class'=>'randomPosts',
'useul'=>1);
$r = wp_parse_args($args,$default);
extract($r);
$r['orderby'] = 'rand';
$r['post__not_in'] = $post_not_in;
$rand_query = new WP_Query($r);
$output = '';
$post_ids = array();
foreach($rand_query->posts as $p){
$post_ids[] = $p->ID;
$output .= '<li><a href="'.get_permalink($p).'">'.get_the_title($p->ID).'</a></li>';
}
if($useul)
$output = '<ul>' .$output . '</ul>';
echo $output;
return $post_ids;
}
function the_related_posts($args = ''){
global $post;
$default = array('showposts'=>10,
'class'=>'relatedPosts');
$r = wp_parse_args($args,$default);
extract($r);
echo '<ul>';
$tags = array();
$terms = get_the_tags();
foreach($terms as $term){
$tags[] = $term->name;
}
if(!empty($tags)){
$tag = implode(",",$tags);
$post_not_in[] = $post->ID;
$post_ids = the_rand_posts("useul=0&showposts=$showposts&tag=$tag",$post_not_in);
$post_not_in = array_merge($post_not_in,$post_ids);
$count = count($post_ids);
}
if($count < $showposts){
$showposts -= $count;
$categorys = get_the_category();
foreach($categorys as $category){
$cats[] = $category->term_id;
}
$cat = implode(",",$cats);
$post_ids = the_rand_posts("useul=0&showposts=$showposts&cat=$cat",$post_not_in);
$count = count($post_ids);
$post_not_in = array_merge($post_not_in,$post_ids);
}
if($count < $showposts){
$showposts -= $count;
the_rand_posts("useul=0&showposts=$showposts",$post_not_in);
}
echo '</ul>';
}
如果你懒得分析代码,可以把以上完整的代码复制到你正在使用主题文件夹下的functions.php文件中,然后在single.php文件中调用
<?php if(function_exists('the_related_posts')) {the_related_posts();} ?>
或者继续关注本站,不日将发布”随机文章”插件的升级版本,集成“相关文章”功能。
The End
phoenixwright
2009-11-23 星期一 20:31
软件开发
2009-11-24 星期二 01:42
DDBug
2009-12-01 星期二 22:12
软件
2009-11-24 星期二 02:47
六月成人
2009-11-30 星期一 23:52
keke
2010-09-03 星期五 13:55
tiny
2010-02-07 星期天 19:23
aserlee
2010-05-08 星期六 17:37
G字头老大
2010-05-16 星期天 09:09
mark
2010-06-12 星期六 12:42
阿达
2010-08-31 星期二 19:57
keke
2010-09-03 星期五 17:07
亿品元素
2010-09-03 星期五 18:58
阿达
2010-09-07 星期二 19:35