不用插件打造自己的存档页
首先声明,这篇文章的源代码来自“偶爱偶家”,本人从“不务正业”处取得源文件下载,呵,不过鉴于两处的下载似乎都链接不上,且原文上并没有直接给出源代码,那么我在这就直接贴出来,为喜欢的朋友省些力气吧。不过我在原代码结构上稍做了一些调整,主要为了能够支持折起与打开的功能,整体看比较类似mg12大人的那个wp_easyarchives插件,虽然不支持年份的选择下拉表,但我个人感觉还是很整齐的。
第一步:制作一个函数在functions.php,您可以直接复制如下代码
- function archives_list_SHe() {
- global $wpdb,$month;
- $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");
- $output = get_option('SHe_archives_'.$lastpost);
- if(empty($output)){
- $output = '';
- $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE 'SHe_archives_%'");
- $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";
- $monthresults = $wpdb->get_results($q);
- if ($monthresults) {
- foreach ($monthresults as $monthresult) {
- $thismonth = zeroise($monthresult->month, 2);
- $thisyear = $monthresult->year;
- $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";
- $postresults = $wpdb->get_results($q);
- if ($postresults) {
- $text = sprintf('%s %d', $month[zeroise($monthresult->month,2)], $monthresult->year);
- $postcount = count($postresults);
- $output .= '<dl><dt><strong>' . $text . '</strong> (' . count($postresults) . ' ' . __('Posts','freephp') . ')</dt>' . "\n";
- foreach ($postresults as $postresult) {
- if ($postresult->post_date != '0000-00-00 00:00:00') {
- $url = get_permalink($postresult->ID);
- $arc_title = $postresult->post_title;
- if ($arc_title)
- $text = wptexturize(strip_tags($arc_title));
- else
- $text = $postresult->ID;
- $title_text = __('View this post','freephp') . ', "' . wp_specialchars($text, 1) . '"';
- $output .= '<dd><em>' . mysql2date('m/d', $postresult->post_date) . '</em>: ' . "<a href='$url' title='$title_text'>$text</a>";
- $output .= ' (' . $postresult->comment_count . ')';
- $output .= '</dd>' . "\n";
- }
- }
- }
- $output .= '</dl>' . "\n";
- }
- update_option('SHe_archives_'.$lastpost,$output);
- }else{
- $output = '<div class="errorbox">'. __('Sorry, no posts matched your criteria.','freephp') .'</div>' . "\n";
- }
- }
- echo $output;
- }
第二步:添加如下JS(我这个使用了jQuery库,如果您不喜欢使用此库的话,那么这部份控制折起与打开的JS代码您可以在网上搜索相应的不需jQuery库的。)
- $(document).ready(function() {
- $("#archlist dt").each(function(){
- var dt = $(this);
- var objList =new Array();
- var tmpDD = dt.next();
- while(tmpDD.attr("tagName")!=null && tmpDD.attr("tagName")!="DT")
- {
- objList.push(tmpDD);
- tmpDD = tmpDD.next();
- }
- dt.toggle(
- function(){
- $(objList).each(function(){
- $(this).show();
- });
- },
- function(){
- $(objList).each(function(){
- $(this).hide();
- });
- }
- );
- });
- });
第三步:制作archives.php模板页
- <?php
- /*
- Template Name: Archives
- */
- ?>
- <?php get_header(); ?>
- <div id="content">
- <h2 class="posttitle"><?php _e('Archives:', 'freephp'); ?></h2>
- <ul id="archlist">
- <?php archives_list_SHe(); ?>
- </ul>
- </div>
- <?php get_sidebar(); ?>
- <?php get_footer(); ?>
OK,大功告成,去建立一个自己的存档页面吧。当然,您或许还需要通过CSS去修饰一下这个页面,呵,不过那我就帮不上什么忙了,怕是越帮越忙。

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