Smart Archives 增强

安装了 Justin Blanton 开发的 smart archives 插件, 发现这个插件对中文和韩文支持不佳。我给做了点小修改,并且对他的一些小缺陷也进行了补正。最后的效果请看 http://www.doyj.com/archives/

安装方法如下:
  1. 将下面的代码拷贝并存成文件smartarchives.php
  2. 将smartarchives.php 上载到你的plugin目录
  3. 到控制面板激活Smart Archives 插件
  4. 在适当的地方插入smartArchives()函数调用。
    这个函数有两个参数,第一个在”both”, “block”,”list”中选择,默认为both 第二个是要排除的分类id.
下面就是修改后的代码:
  1. <?php
  2. /*
  3. Plugin Name: Smart Archives
  4. Version: 1.5
  5. Plugin URI: http://justinblanton.com/projects/smartarchives/
  6. Description: A simple, clean, and future-proof way to present your archives.
  7. Author: Justin Blanton
  8. Author URI: http://justinblanton.com
  9. */
  10.  
  11. function smartArchives($format='both', $catID='') {
  12.  
  13. global $tableposts, $wpdb, $PHP_SELF , $month ;
  14. setlocale(LC_ALL,WPLANG); // set localization language
  15. // set the URI of your archives; this might not need to be changed (currently, it's http://yoursite.com/archives/)
  16. $now = gmdate('Y-m-d H:i:s'); // get the current GMT date
  17.  
  18. if (($format == 'both') || ($format == 'block')) { // check to see if we are supposed to display the block
  19.     $qy = mysql_query("SELECT distinct year(post_date) as year, post_status
  20.         FROM $tableposts 
  21.         WHERE post_status='publish'
  22.         AND post_date <= NOW()
  23.         ORDER BY year desc");
  24.     // loop to create the small archive block with year/month links
  25.     while($years = mysql_fetch_array($qy)) {
  26.     echo('<strong><a href="'.get_year_link($years[year]).'">'.$years[year].'</a>:</strong> ');
  27.         $qm = mysql_query("SELECT distinct month(post_date) as monthv
  28.             FROM $tableposts 
  29.             ORDER BY monthv  asc") or die(mysql_error());
  30.            
  31.             for ($i=1; $i<=12; $i++)
  32.             {
  33.                     $q = mysql_query("SELECT *, year(post_date) as year
  34.                     FROM $tableposts 
  35.                     WHERE year(post_date)='$years[year]'
  36.                     AND month(post_date)='$i'
  37.                     AND post_status='publish'
  38.                     AND post_date <= NOW()
  39.                     ORDER BY id desc") or die(mysql_error());
  40.                     $sm =    $month[zeroise($i,2)]; // get the shortened month name; strtotime() localizes
  41.  
  42.                     if(mysql_num_rows($q)) 
  43.                         { echo('<a href="'.get_month_link( $years[year], $i ).'">'.$sm.'</a> '); }
  44.                     else 
  45.                         { echo('<span class="emptymonth">'.$sm.'</span> '); }
  46.             }
  47.            
  48.             echo('<br />');
  49.     }
  50.     echo ('<br /><br />');
  51. }
  52.  
  53. if (($format == 'both') || ($format == 'list')) { //check to see if we are supposed to display the list
  54.     $qy = mysql_query("SELECT distinct year(post_date) as year, post_status
  55.         FROM $tableposts 
  56.         WHERE post_status='publish'
  57.         AND post_date <= NOW()
  58.         ORDER BY year desc");
  59.     // loop to display links to all posts, sorted by descending month and day
  60.     while($years = mysql_fetch_array($qy)) {
  61.         $qm = mysql_query("SELECT distinct month(post_date) as monthv FROM $tableposts ORDER BY monthv desc") or die(mysql_error());
  62.         while($date = mysql_fetch_array($qm)) {
  63.             $q = mysql_query("SELECT *, year(post_date) as year, month(post_date) as monthv
  64.                 FROM $tableposts WHERE year(post_date)='$years[year]'
  65.                 AND month(post_date)='$date[monthv]'
  66.                 AND post_status='publish'
  67.                 AND post_date <= NOW()
  68.                 ORDER BY id desc") or die(mysql_error());
  69.             if(mysql_num_rows($q)) {
  70.                 $lm = $month[zeroise($date[monthv],2)]; // get the full month name; strtotime() localizes
  71.                 echo('<h2><a href="'.get_month_link( $years[year], $date[monthv] ).'">'.$lm.' '.$years[year].'</a></h2>');
  72.                 echo('<ul>');
  73.                 $q = mysql_query("SELECT *, year(post_date) as year, month(post_date) as monthv
  74.                     FROM $tableposts WHERE year(post_date)='$years[year]'
  75.                     AND month(post_date)='$date[monthv]'
  76.                     AND post_status='publish'
  77.                     ORDER BY post_date desc") or die(mysql_error());
  78.                 while($post = mysql_fetch_array($q)) {
  79.                     if ($post[post_date_gmt] <= $now) {
  80.                         if ($catID != '') { // check to see if a category id was specified in the arguments
  81.                             // get the categories that are attached to the current post
  82.                             $cats = $wpdb->get_col("SELECT category_id FROM $wpdb->post2cat WHERE post_id = $post[ID]");
  83.                             $found=false;
  84.                             foreach ($cats as $cat) { // look to see if the specified category is attached to the current post
  85.                                  if ($cat == $catID) $found=true;
  86.                                }
  87.                                if (!$found) echo('<li><a href="'.get_permalink($post[ID]).'">'.$post[post_title].'</a></li>');
  88.                         }
  89.                         else echo('<li><a href="'.get_permalink($post[ID]).'">'.$post[post_title].'</a></li>');
  90.                     }
  91.                 }
  92.                 echo ('</ul><br />');
  93.             }
  94.         }
  95.     }
  96. }
  97. }
  98. ?>

Tags: ,

categories IT

5 条评论

  • By alu, 十二月 22, 2006 @ 11:03 上午

    smartArchives()函数调用。,能不能具体写出来?我怎么试也不可以啊…汗

    [回复]

  • By oldmonk, 十二月 22, 2006 @ 1:47 下午

    在评论里不能写代码,给你email过去了

    [回复]

  • By Albert, 六月 2, 2007 @ 6:21 下午

    不错,不错,修复了乱码问题,但我复制的时候把前面的序号也复制进来了,还要一个一个的删。建议把前面的序号去掉。

    [回复]

  • By Albert, 六月 4, 2007 @ 4:13 下午

    发现一个问题,我不能用win live writer发布日志,试了半天之后才明白,是因为这个插件造成的故障,通用smart archive之后live writer一切正常。

    [回复]

Other Links to this Post

  1. Smart Archives 增强代码 | IdeaTalks — 二月 2, 2008 @ 4:12 下午

这篇文章上的评论的 RSS feed TrackBack URI

留下评论

  • :em41:
  • :em17:
  • :em10:
  • :em43:
  • :em49:
  • :em22:
  • :em25:
  • :em44:
  • :em27:
  • :em50:
  • :em03:
  • :em72:
  • :em71:
  • :em19:
  • :em65:
  • :em15:
  • :em34:
  • :em51:
  • :em09:
  • :em12:
  • :em26:
  • :em42:
  • :em58:
  • :em39:
  • :em33:
  • :em70:
  • :em05:
  • :em48:
  • :em11:
  • :em20:
  • :em57:
  • :em24:
  • :em60:
  • :em30:
  • :em06:
  • :em13:
  • :em36:
  • :em56:
  • :em67:
  • :em23:
  • :em63:
  • :em37:
  • :em16:
  • :em28:
  • :em38:
  • :em45:
  • :em04:
  • :em08:
  • :em21:
  • :em69:
  • :em47:
  • :em40:
  • :em31:
  • :em01:
  • :em68:
  • :em32:
  • :em29:
  • :em18:
  • :em14:
  • :em66:
  • :em35:
  • :em54:
  • :em02:
  • :em55:
  • :em64:
  • :em61:
  • :em46:
  • :em62:
  • :em52:
  • :em07:
  • :em59:
  • :em53:

使用新浪微博登陆