之前在【纯代码实现Thesis主题相关文章功能】一文介绍了试用于Thesis主题的带缩略图的相关文章代码,对于其他主题则不适用。有没有WordPress通用的相关文章代码呢?

我爱水煮鱼的WordPress Related Posts样式
下面是从【我爱水煮鱼的WordPress Related Posts】插件中提取出来的代码,可以实现如上图所示的相关文章效果。以列表形式显示自定义树木的相关文章,根据Tag匹配,如果找不到匹配的相关文章,则显示自定义数量的随机文章。支持在feed中显示,支持文章发布日期、评论数显示。可以选择标题字号:h2,h3,h4...
将以下代码添加到function.php文件中即可:
//WordPress Related Posts
$wp_rp=array(
'limit'=>8, //相关文章数量
'wp_rp_rss'=>true, //是否在feed 中显示相关文章
'wp_no_rp'=>'random', //无相关文章时的选择:text 或random(random-随机文章)
'wp_rp_date'=>true, //显示文章发布日期
'wp_rp_comments'=>true, //显示文章评论数
'wp_rp_title_tag'=>'h2', //选择相关文章标题标签(h2 ,h3 ,h4 ,p ,div)
);
function wp_get_random_posts ($limitclause="") {
global $wpdb, $post;
$q = "SELECT ID, post_title, post_content,post_excerpt, post_date, comment_count FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND ID != $post->ID ORDER BY RAND() $limitclause";
return $wpdb->get_results($q);
}
function wp_get_related_posts()
{
global $wpdb, $post,$wp_rp;
$limit =$wp_rp["limit"];
$wp_rp_title='Related Posts'; //相关文章标题
if(!$post->ID){return;}
$now = current_time('mysql', 1);
$tags = wp_get_post_tags($post->ID);
$taglist = "'" . $tags[0]->term_id. "'";
$tagcount = count($tags);
if ($tagcount > 1) {
for ($i = 1; $i < $tagcount; $i++) {
$taglist = $taglist . ", '" . $tags[$i]->term_id . "'";
}
}
if ($limit) {
$limitclause = "LIMIT $limit";
} else {
$limitclause = "LIMIT 10";
}
$q = "SELECT p.ID, p.post_title, p.post_content,p.post_excerpt, p.post_date, p.comment_count, count(t_r.object_id) as cnt FROM $wpdb->term_taxonomy t_t, $wpdb->term_relationships t_r, $wpdb->posts p WHERE t_t.taxonomy ='post_tag' AND t_t.term_taxonomy_id = t_r.term_taxonomy_id AND t_r.object_id = p.ID AND (t_t.term_id IN ($taglist)) AND p.ID != $post->ID AND p.post_status = 'publish' AND p.post_date_gmt < '$now' GROUP BY t_r.object_id ORDER BY cnt DESC, p.post_date_gmt DESC $limitclause;";
$related_posts = $wpdb->get_results($q);
$output = "";
//不存在相关日志则显示随机日志
if (!$related_posts)
{
if($wp_rp['wp_no_rp'] == "text")
{
$output .= '<li>No Related Posts</li>'; //无相关文章时显示标题
}
else
{
if($wp_rp['wp_no_rp'] == "random")
{
$wp_no_rp_text= 'Ramdom Posts'; //随机文显示标题
$related_posts = wp_get_random_posts($limitclause);
}
$wp_rp_title = $wp_no_rp_text;
}
}
foreach ($related_posts as $related_post )
{
$output .= '<li>';
if($wp_rp['wp_rp_date'])
{
$dateformat = get_option('date_format');
$output .= mysql2date($dateformat, $related_post->post_date) . " — "; //日期和文章标题间隔符,默认是 —
}
$output .= '<a href="'.get_permalink($related_post->ID).'" title="'.wptexturize($related_post->post_title).'">'.wptexturize($related_post->post_title).'</a>';
if ($wp_rp["wp_rp_comments"])
{
$output .= " (" . $related_post->comment_count . ")";
}
$output .= '</li>';
}
$output = '<ul>' . $output . '</ul>';
$wp_rp_title_tag = $wp_rp["wp_rp_title_tag"];
if(!$wp_rp_title_tag)
$wp_rp_title_tag ='h3';
if($wp_rp_title != '')
$output = '<'.$wp_rp_title_tag.' >'.$wp_rp_title .'</'.$wp_rp_title_tag.'>'. $output;
return $output;
}
function wp_related_posts_attach($content)
{
global $wp_rp;
if (is_single()||(is_feed() && $wp_rp["wp_rp_rss"]))
{
$output = wp_get_related_posts();
$content = $content.$output;
}
return $content;
}
add_filter('the_content', 'wp_related_posts_attach',100);
您可能也喜欢这些文章:
- 2011/02/10 — 纯代码实现Wordpress彩色标签云 (7)
- 2011/06/17 — 纯代码实现外链自动添加小图标 (7)
- 2011/02/10 — Wordpress技巧—从导航菜单移除特定页面 (6)
- 2012/03/29 — 纯代码替换All in One SEO Pack 插件,实现SEO优化 (7)
- 2012/03/25 — 【Infographic】解剖WordPress主题的结构、功能 (3)
- 2012/03/16 — WordPress配置文件wp-config.php实用技巧 (3)
- 2011/09/29 — WordPress网站.htaccess文件配置技巧 (4)
- 2011/07/02 — 纯代码实现WordPress热门文章功能 (6)

{ 10 Comments }
固定个数的相关文章代码搞出来了吗?
I am constantly invstigating online for ideas that can facilitate me. Thanks!
Simply desire to say your article is as surprising. The clearness in your post is simply cool and i can assume you are an expert on this subject. Fine with your permission allow me to grab your feed to keep up to date with forthcoming post. Thanks a million and please carry on the enjoyable work.
博客很好,纯代码资源很多,收藏了。
请教下如果我不想自动在文章末尾插入改怎么调用
把 function wp_related_posts_attach($content)及后面的代码删掉。然后在WP模板文件中你想显示相关文章的位置插入 < ?php wp_related_posts(); ?> 模板函数
谢谢啦!
晕~你代码里可没有wp_related_posts()这个函数啊,插上有用么~
没有成功哦~ 说一声~ 可能是主题不支持 用的imotta主题
啥症状?是未显示相关文章,还是直接死掉?
{ 3 trackbacks }