也折腾不用WordPress Related Posts实现相关日志
Posted by 荒野无灯 @ 十二月 2, 2009 at 2:57 下午 under wp技巧 39 Comments
无插件实现“相关日志”的显示,代码为本人修改自WordPress Related Posts插件 ,无相关日志时显示随机日志(或显示“无相关日志”,可配置)。
使用效果如本博所示。和我一样喜欢折腾的朋友拿去吧:-)
使用方法:
方法1
直接下载:
wp_related_posts_hywd.zip |
文件大小:1.6 KB |
| 更新时间:2010年06月24日 | 累计下载:52 次 |
| MD5 验证:23caa38fa46932e7b439860652287a02 | [MD5验证工具下载] |
1 | require_once('wp_related_posts_hywd.php'); |
或者
方法二:
将以下代码添加到你目前使用的主题的functions.php文件的最后一个?>标签前面即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | ####### START ########## 显示相关日志 ################# START ######### ########################### www.ihacklog.com ###################################### $wp_rp=array( 'limit'=>6, //显示几条相关文章 'wp_rp_rss'=>true, //在rss feed 中显示相关文章 'wp_no_rp'=>'random', //无相关文章时的选择:text 或random (random为显示随机文章) 'wp_rp_date'=>true, //显示日志发布日期 'wp_rp_comments'=>true, //显示日志评论数 'wp_rp_title_tag'=>'h3',//相关日志标题标签(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='相关日志'; 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>无相关日志</li>'; } else { if($wp_rp['wp_no_rp'] == "random") { $wp_no_rp_text= '随机日志'; $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 class="related_post">' . $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.' class="related_post_title">'.$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); ####### END ########## 显示相关日志 ################# END ######### ########################### www.ihacklog.com ###################################### |
喜欢这篇文章吗?
请订阅本站 RSS feed
作者:荒野无灯
出处:Hacklog【荒野无灯weblog】
出处:Hacklog【荒野无灯weblog】
声明: 本站遵循 署名-非商业性使用-相同方式共享 2.5 共享协议. 转载请注明转自Hacklog【荒野无灯weblog】
本文链接: http://www.ihacklog.com/wordpress/wpskills/wordpress-related-posts-with-no-plugin.html
相关日志
Comments (39)




请问怎样让文章下面左边显示相关日志,右边显示随机日志呢。。。因为这个代码也有随机日志的函数,怎样直接调用使用吗?
直接调用不行的,要自己写一个函数吧。
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
$related_posts = wp_get_random_posts($limitclause);
foreach ($related_posts as $related_post )
{
$output .= '<li>';
$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>';
$output .= " (" . $related_post->comment_count . ")";
$output .= '</li>';
}
$output = '<h3>随机日志</h3><ul class="related_post">' . $output . '</ul>';
echo $output;
}
//调用 :
output_random_posts(‘LIMIT 10’);
这个我用的Willin大叔的,很赞!!
[...] 4. 无插件显示相关日志:这个是荒野无灯的作品,题目就很明白了,就是那个意思…… [...]