这个方法取自网上,被转载了很多,无法标识来源了。仅写下来记录以后备用。
使用成功后,会在数据库生成一个表SHe_archives_25216,这是保存文章数据,以免每次查询耗费时间。
1.把以下 archives_list_SHe()函数放进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 .= '<ul class="archives-list"><li><span class="archives-yearmonth">' . $text . ' (' . count($postresults) . ' 篇文章)</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, "' . 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.</div>' . "n";
- }
- }
- echo $output;
- }
2.把主题里的page.php另存为archives.php,然后在开头修改为
- <?php
- /*
- Template Name: archives
- */
- ?>
然后在archives.php里面找到类似<?php content(); ?>的语句,在wordpresss 3.1版本中,就寻找
<?php get_template_part( 'content', 'page' ); ?>
然后添加
- <a id="expand_collapse" href="#">全部展开/收缩</a>
- <div id="archives"><?php archives_list_SHe(); ?></div>
在后台新建页面,选择模版archives
3.添加滑动伸缩效果,倘若网站没有加载过jQuery库,把下面代码添加到archives页面即可。
- <script src=http://ajax.lug.ustc.edu.cn/ajax/libs/jquery/1.4.2/jquery.min.js type=text/javascript></script>
- <script type="text/javascript">
- jQuery(document).ready(
- function($){
- $('#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');
- });
- });
- </script>
当然你也可以把库文件下载到网站目录,自己调用。
PS,试用过程用、中,我出现显示不正常的问题,只显示十一月2011字样,之后全部没显示,最后发现是数据库里生成的SHe_archives_25216表出现问题,删除后即恢复正常
版权声明:本文为原创文章,版权归 neo 所有。
本作品采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可。
0 条评论