代码实现WordPress归档页面模板[速度很快]
以前使用Clean Archives Reloaded插件实现博客文章归档,但是插件太多了影响网站速度,今天从ZWWoOoOo看到了如何使用纯代码实现文章归档,很不错,借来用用。
该代码特点:
1. 这个存档函数会在数据库生成一个表 SHe_archives_25216,用来保存在文章新发表/删除文章时生成的 html,这主要是加快访问速度,不用每次都要查询数据库生成归档。
2. 显示每月文章数
3. 显示每篇文章的评论数
使用效果:见我博客的存档页
使用说明:我在代码的基础上另外加了jQuery滑动效果。
下面是详细步骤:
1. 把下面的 archives_list_SHe 函数代码扔进主题的 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 | 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 .= '<ul class="archives-list"><li><span class="archives-yearmonth">' . $text . ' (' . count($postresults) . ' ' . __('篇文章','freephp') . ')</span><ul class="archives-monthlisting">' . "\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 .= '<li>' . mysql2date('d日', $postresult->post_date) . ': ' . "<a href="$url" title="$title_text">$text</a>"; $output .= ' (' . $postresult->comment_count . ')'; $output .= '</li>' . "\n"; } } } $output .= '</ul></li></ul>' . "\n"; } update_option('SHe_archives_'.$lastpost,$output); }else{ $output = '<div class="errorbox">'. __('Sorry, no posts matched your criteria.','freephp') .'</div>' . "\n"; } } echo $output; } |
2. 复制一份主题的 page.php 更名为 archives.php,然后在最顶端加入:
1 2 3 4 5 | <?php /* Template Name: archives */ ?> |
再然后找到类似 <?php content(); ?>,在其下面加入如下代码
1 2 3 4 | <p><?php bloginfo('name'); ?>创建以来已经播下 <?php $count_posts = wp_count_posts(); echo $published_posts = $count_posts->publish;?> 颗种子,欢迎查阅:</p> <a id="expand_collapse" href="#">展开/收缩所有月份</a> <div id="archives"><?php archives_list_SHe(); ?></div> |
进入WordPress后台添加一存档新页面(或修改目前的存档页面),在右侧栏模板选择archives。
3. 如果你的主题本身加载了 jQuery 库,那么继续把下面的 jQ 代码加上去,就会有滑动伸缩效果了。
1 2 3 4 5 6 7 8 9 10 | $('#expand_collapse,.archives-yearmonth').css({cursor:"s-resize"}); $('#archives ul li ul.archives-monthlisting').hide(); $('#archives ul li ul.archives-monthlisting:first').show(); $('#archives ul li span.archives-yearmonth').click(function(){$(this).next().slideToggle('fast');return false;}); //以下下是全局的操作 $('#expand_collapse').toggle(function(){ $('#archives ul li ul.archives-monthlisting').slideDown('fast'); },function(){ $('#archives ul li ul.archives-monthlisting').slideUp('fast'); }); |
CSS样式可以通过 #archive 来定义。
从折腾WordPress就一直使用的存档页面插件 Clean Archives Reloaded 终于可以光荣退休了。
OK,折腾完~~~
   

iplaywind兄,现在很厉害啊!
@tomheng 见笑见笑,还需要多多学习。
先收藏下来 等以后文章多了 再弄个…
把下面的 archives_list_SHe 函数代码扔进主题的 functions.php那个地方啊!
@Yep archives_list_SHe是一个单独的函数,你只需要放到functions.php中的最后的其他函数中间即可。
@yalonzh archives_list_SHe是一个单独的函数,你只需要放到functions.php中的最后的其他函数中间即可。
有个什么插件归档很好用,不会把所有链接放一个页面上,会分页的。叫什么一下子忘了。。汗。。
上面代码还有的没有转义,让我们php盲情何以堪啊。。。js代码也不贴出来呢。。