代码实现WordPress文章归档

这个方法取自网上,被转载了很多,无法标识来源了。仅写下来记录以后备用。

使用成功后,会在数据库生成一个表SHe_archives_25216,这是保存文章数据,以免每次查询耗费时间。

1.把以下 archives_list_SHe()函数放进functions.php里面

  1. function archives_list_SHe() {
  2. global $wpdb,$month;
  3. $lastpost = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_date <'" . current_time('mysql') . "' AND post_status='publish' AND post_type='post' AND post_password='' ORDER BY post_date DESC LIMIT 1");
  4. $output = get_option('SHe_archives_'.$lastpost);
  5. if(empty($output)){
  6. $output = '';
  7. $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE 'SHe_archives_%'");
  8. $q = "SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, count(ID) as posts FROM $wpdb->posts p WHERE post_date <'" . current_time('mysql') . "' AND post_status='publish' AND post_type='post' AND post_password='' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC";
  9. $monthresults = $wpdb->get_results($q);
  10. if ($monthresults) {
  11. foreach ($monthresults as $monthresult) {
  12. $thismonth = zeroise($monthresult->month, 2);
  13. $thisyear = $monthresult->year;
  14. $q = "SELECT ID, post_date, post_title, comment_count FROM $wpdb->posts p WHERE post_date LIKE '$thisyear-$thismonth-%' AND post_date AND post_status='publish' AND post_type='post' AND post_password='' ORDER BY post_date DESC";
  15. $postresults = $wpdb->get_results($q);
  16. if ($postresults) {
  17. $text = sprintf('%s %d', $month[zeroise($monthresult->month,2)], $monthresult->year);
  18. $postcount = count($postresults);
  19. $output .= '<ul class="archives-list"><li><span class="archives-yearmonth">' . $text . ' &nbsp;(' . count($postresults) . '&nbsp;篇文章)</span><ul class="archives-monthlisting">' . "n";
  20. foreach ($postresults as $postresult) {
  21. if ($postresult->post_date != '0000-00-00 00:00:00') {
  22. $url = get_permalink($postresult->ID);
  23. $arc_title = $postresult->post_title;
  24. if ($arc_title)
  25. $text = wptexturize(strip_tags($arc_title));
  26. else
  27. $text = $postresult->ID;
  28. $title_text = 'View this post, &quot;' . wp_specialchars($text, 1) . '&quot;';
  29. $output .= '<li>' . mysql2date('d日', $postresult->post_date) . ':&nbsp;' . "<a href='$url' title='$title_text'>$text</a>";
  30. $output .= '&nbsp;(' . $postresult->comment_count . ')';
  31. $output .= '</li>' . "n";
  32. }
  33. }
  34. }
  35. $output .= '</ul></li></ul>' . "n";
  36. }
  37. update_option('SHe_archives_'.$lastpost,$output);
  38. }else{
  39. $output = '<div class="errorbox">Sorry, no posts matched your criteria.</div>' . "n";
  40. }
  41. }
  42. echo $output;
  43. }

2.把主题里的page.php另存为archives.php,然后在开头修改为

  1. <?php  
  2. /*  
  3. Template Name: archives  
  4. */ 
  5. ?> 

然后在archives.php里面找到类似<?php content(); ?>的语句,在wordpresss 3.1版本中,就寻找

<?php get_template_part( 'content', 'page' ); ?>

然后添加

  1. <a id="expand_collapse" href="#">全部展开/收缩</a>
  2. <div id="archives"><?php archives_list_SHe(); ?></div>

在后台新建页面,选择模版archives

3.添加滑动伸缩效果,倘若网站没有加载过jQuery库,把下面代码添加到archives页面即可。

  1. <script src=http://ajax.lug.ustc.edu.cn/ajax/libs/jquery/1.4.2/jquery.min.js type=text/javascript></script>  
  2. <script type="text/javascript">  
  3. jQuery(document).ready(  
  4. function($){  
  5. $('#expand_collapse,.archives-yearmonth').css({cursor:"s-resize"});  
  6. $('#archives ul li ul.archives-monthlisting').hide();  
  7. $('#archives ul li ul.archives-monthlisting:first').show();  
  8. $('#archives ul li span.archives-yearmonth').click(function(){$(this).next().slideToggle('fast');return false;});  
  9. $('#expand_collapse').toggle(  
  10.  function(){  
  11. $('#archives ul li ul.archives-monthlisting').slideDown('fast');  
  12. },  
  13.  function(){  
  14. $('#archives ul li ul.archives-monthlisting').slideUp('fast');  
  15. });  
  16. });  
  17. </script> 

当然你也可以把库文件下载到网站目录,自己调用。

PS,试用过程用、中,我出现显示不正常的问题,只显示十一月2011字样,之后全部没显示,最后发现是数据库里生成的SHe_archives_25216表出现问题,删除后即恢复正常

0 条评论