改造WordPress Related Posts按分类检索
WordPress Related Posts是一个国人开发的,按tag检索相关日志的wordpress插件。
使用该插件的前提是,你的文章中必须有加入tag标签。
对于某些人来说,好不容易写完一篇日志,还要从正文中抽取tag是一件非常痛苦的事情。(别跟我说使用XX seo插件,这类插件精准度太低,还不如不用)
定期重建tag更是耗费精力。
本文旨在帮助那些没有使用tag,但却想在wordpress中加入相关日志的朋友。
我的相关日志检索方案是利用日志所属分类,随机查询分类下的日志并按发表时间从现在到以前排序实现的。
这个方案,需要修改部分Wordpress Related Posts插件的代码,不过很简单.
首先,用文本编辑器打开插件目录下的 wp_related_posts.php
第一步
找到
$tags = wp_get_post_tags($post->ID); $taglist = "'" . $tags[0]->term_id. "'";
替换为
$tags = wp_get_post_categories($post->ID, array('all_with_object_id')); $taglist = "'" . $tags[0]. "'";
第二步
找到
$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;";
替换为
$q = "SELECT p.ID, p.post_title, p.post_content,p.post_excerpt, p.post_date, p.comment_count FROM $wpdb->term_taxonomy t_t, $wpdb->term_relationships t_r, $wpdb->posts p WHERE t_t.taxonomy ='category' 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' ORDER BY RAND() DESC, p.post_date_gmt DESC $limitclause;";
大功告成,上传到服务器上试一试吧!
作者:Ken Wu 原文链接:http://kenwublog.com/wordpress-related-posts-select-by-category