avatar

WordPress Related Post 代码

根据自己的需求写了一份 Related Post 的代码
如果运气好一条查询语句就可以将结果查询出来了, 最差3条可以完事

如果觉得那里有问题 请一定要告诉我啊!

function build_related_post(&$post_ids, $posts) 方法应该是这样的
这里被wordpress给转义掉了, 新主题应该会修复这个问题
已修复

使用说明

在需要的地方加入以下语句即可注1

工作原理

首先根据文章的Tags进行搜索, 按命中Tags次数进行排序, 获取前五条注2, 如果不满五条, 然后根据文章分组进行查询, 随机在结果集里面抽取, 凑满五条记录, 如果还是不满五条的话, 那么只能无条件的进行随机查询了, 最终获取五条记录, 并保证这五条记录不会出现重复情况!

更新日志

== 2012-09-22 ==

修复一个小bug

== 2012-09-07 ==

删除了一个无用的返回值

== 2012-09-03 ==

第一个版本

具体实现

function tp_related_post($post_id) {
  $chain = '';
  $size = 5;
  global $tags;
  global $categories;
  $permalink = get_option('permalink_structure');
  $category = $categories[0];
  $chain .= '
    '; $post_ids = array($post_id); if (!empty($tags)) { foreach ($tags as $tag) { $ids .= $tag->term_taxonomy_id . ', '; } $posts = get_related_post_id_by_tags($ids, $post_id, $size); $chain .= build_related_post($post_ids, $posts, $permalink); } if (count($post_ids) <= $size) { $posts = get_related_post_id_by_category($post_ids, $category, ($size - count($post_ids))); $chain .= build_related_post($post_ids, $posts, $permalink); } if (count($post_ids) <= $size) { $posts = get_related_post_by_rand($post_ids, ($size - count($post_ids))); $chain .= build_related_post($post_ids, $posts, $permalink); } $chain .= '
'; echo $chain; } function build_related_post(&$post_ids, $posts, $permalink) { $chain = ''; if (!empty($posts)) { foreach ($posts as $post) { $chain .= '
  • '; if (empty($permalink)) $chain .= ''; else $chain .= 'post_name, $permalink)) . '" title="View this post ' . $post->post_title . '">'; $chain .= $post->post_title . ''; $chain .= '
  • '; array_push($post_ids, $post->ID); } } return $chain; } function get_related_post_by_rand($post_ids, $limit) { global $wpdb; $query = 'SELECT t1.ID, t1.post_name, t1.post_title '; $query .= 'FROM '; $query .= $wpdb->posts . ' AS t1 '; $query .= 'WHERE t1.ID NOT IN (' . implode(', ', $post_ids) . ') '; $query .= 'AND t1.post_status = "publish" '; $query .= 'AND t1.post_type = "post" '; $query .= 'ORDER BY '; $query .= 'RAND() '; $query .= 'LIMIT ' . ($limit + 1); return $wpdb->get_results($query); } function get_related_post_id_by_category($post_ids, $category, $limit) { global $wpdb; $query = 'SELECT t1.ID, t1.post_name, t1.post_title '; $query .= 'FROM '; $query .= $wpdb->posts . ' AS t1, '; $query .= $wpdb->term_relationships .' AS t2 '; $query .= 'WHERE t1.ID NOT IN (' . implode(', ', $post_ids) . ') '; $query .= 'AND t1.ID = t2.object_id '; $query .= 'AND t2.term_taxonomy_id =' . $category->term_taxonomy_id . ' '; $query .= 'AND t1.post_status = "publish" '; $query .= 'AND t1.post_type = "post" '; $query .= 'GROUP BY t1.ID '; $query .= 'ORDER BY '; $query .= 'RAND() '; $query .= 'LIMIT ' . ($limit + 1); return $wpdb->get_results($query); } function get_related_post_id_by_tags($ids, $post_id, $limit) { global $wpdb; $query = 'SELECT t1.ID, t1.post_name, t1.post_title '; $query .= 'FROM '; $query .= $wpdb->posts . ' AS t1, '; $query .= $wpdb->term_relationships .' AS t2 '; $query .= 'WHERE t1.ID != ' . $post_id . ' '; $query .= 'AND t1.ID = t2.object_id '; $query .= 'AND t2.term_taxonomy_id in (' . substr($ids, 0, -2) . ') '; $query .= 'AND t1.post_status = "publish" '; $query .= 'AND t1.post_type = "post" '; $query .= 'GROUP BY t1.ID '; $query .= 'ORDER BY '; $query .= 'count(0) DESC, '; $query .= 't1.post_date DESC '; $query .= 'LIMIT ' . $limit; return $wpdb->get_results($query); }

    注1: 需要有全局的$post变量 一般在single页面中声明过 the_post(); 就行了!
    注2: 显示多少条记录可以通过设置 $size 的数字调整

    Comments ( 0 ) Trackbacks ( 0 ) Leave a Reply
    1. No comments yet.

      目前尚无任何评论.

      コメントはまだありません。

    1. No trackbacks yet.

      目前尚无任何 trackbacks 和 pingbacks.

      トラックバックはまだありません。

    • ☆*:.。. o(≧▽≦)o .。.:*☆
    • _(:з」∠)_
    • ♪(´ε` )
    • ψ(`∇´)ψ
    • (-_-#)
    • (=´∀`)人(´∀`=)
    • \(//∇//)\
    • ♪(*^^)o∀*∀o(^^*)♪
    • (((o(*゚▽゚*)o)))
    • (´・_・`)
    • σ(^_^;)
    • ( *`ω´)
    • (ノ`Д´)ノ
    • (( _ _ ))..zzzZZ
    • ( ̄▽ ̄)
    • ヽ(`Д´#)ノ
    • ((((;゚Д゚)))))))
    • (>_<)
    • (T_T)
    • ( T_T)\(^-^ )
    • ε=ε=ε=ε=ε=ε=┌(; ̄◇ ̄)┘