不用插件打造自己的存档页

archiveslist首先声明,这篇文章的源代码来自“偶爱偶家”,本人从“不务正业”处取得源文件下载,呵,不过鉴于两处的下载似乎都链接不上,且原文上并没有直接给出源代码,那么我在这就直接贴出来,为喜欢的朋友省些力气吧。不过我在原代码结构上稍做了一些调整,主要为了能够支持折起与打开的功能,整体看比较类似mg12大人的那个wp_easyarchives插件,虽然不支持年份的选择下拉表,但我个人感觉还是很整齐的。

第一步:制作一个函数在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 .= '<dl><dt><strong>' . $text . '</strong> &nbsp;(' . count($postresults) . '&nbsp;' . __('Posts','freephp') . ')</dt>' . "\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','freephp') . ', &quot;' . wp_specialchars($text, 1) . '&quot;';
  29.               $output .= '<dd><em>' . mysql2date('m/d', $postresult->post_date) . '</em>:&nbsp;' . "<a href='$url' title='$title_text'>$text</a>";
  30.               $output .= '&nbsp;(' . $postresult->comment_count . ')';
  31.               $output .= '</dd>' . "\n";
  32.             }
  33.           }
  34.         }
  35.         $output .= '</dl>' . "\n";
  36.       }
  37.       update_option('SHe_archives_'.$lastpost,$output);
  38.     }else{
  39.         $output = '<div class="errorbox">'. __('Sorry, no posts matched your criteria.','freephp') .'</div>' . "\n";
  40.     }
  41.   }
  42.   echo $output;
  43. }

第二步:添加如下JS(我这个使用了jQuery库,如果您不喜欢使用此库的话,那么这部份控制折起与打开的JS代码您可以在网上搜索相应的不需jQuery库的。)

  1. $(document).ready(function() {
  2.   $("#archlist dt").each(function(){
  3.     var dt = $(this);
  4.     var objList =new Array();
  5.     var tmpDD = dt.next();
  6.     while(tmpDD.attr("tagName")!=null && tmpDD.attr("tagName")!="DT")
  7.     {
  8.       objList.push(tmpDD);
  9.       tmpDD = tmpDD.next();
  10.     }
  11.     dt.toggle(     
  12.       function(){
  13.         $(objList).each(function(){
  14.           $(this).show();                                  
  15.         })
  16.       },             
  17.       function(){
  18.         $(objList).each(function(){
  19.           $(this).hide();                                  
  20.         })
  21.       }
  22.     );
  23.   });
  24. });

第三步:制作archives.php模板页

  1. <?php
  2. /*
  3. Template Name: Archives
  4. */
  5. ?>
  6. <?php get_header(); ?>
  7.   <div id="content">
  8.     <h2 class="posttitle"><?php _e('Archives:', 'freephp'); ?></h2>
  9.       <ul id="archlist">
  10.         <?php archives_list_SHe(); ?>
  11.       </ul>
  12.   </div>
  13. <?php get_sidebar(); ?>
  14. <?php get_footer(); ?>

OK,大功告成,去建立一个自己的存档页面吧。当然,您或许还需要通过CSS去修饰一下这个页面,呵,不过那我就帮不上什么忙了,怕是越帮越忙。

  1. 2009年9月19日 15:56 | #1

    非常遗憾,我的还是使用了一个插件呢。
    真是汗~~~ :razz:

  2. 2009年9月19日 17:41 | #2

    @365hope
    呵,我这个皮还需要继续搞,还是有不少未知问题的,而且不可能把自己所有要用的插件都放在皮里,只能是将常用的放进来。你才用了一个插件,已经很让人惊奇了,呵,因为我需要一个通用的东西,完全的自我方式只能让别人欣赏,但不能让别人共享。

  3. 2009年11月26日 12:59 | #3

    正在调试中…

emoticons

发表评论