JAVA、PHP、前端、APP、网站开发 - 开发技术学习

开发技术学习 » 编程开发 » wordpress归档页面,wordpress如何建立归档页面

wordpress归档页面,wordpress如何建立归档页面

此文被围观2901日期: 2013-01-31 分类 : 编程开发  标签:  ·····
一致以来都觉得现在有必要建立一个文章归档页面。 随着日志的增多,觉得有必要建立一个专门的文章归档页面,但是很惊奇的发现现用的主题竟然没有这个页面模版,于是打算借鉴一下inove主题中直接支持建立文章归档页面的代码,代码量很少,但每次访问存档页面数据库查询量太大导致打开页面很慢,那么有别的打开该页面速度快的方法吗,很幸运的我在zwwooooo的博客上发现了这个方法,代码俺就直接拿来了哦。 原理是:添加一段函数代码,这个存档函数会在数据库生成一个表 wp_archives_list,用来保存在文章新发表/删除文章时生成的 html,这主要是加快访问速度,不用每次都要查询数据库生成归档。 方法如下: 1.把下面的 wp_archives_list 函数代码扔进主题文件夹下的 functions.php 里面 [php] function wp_archives_list() { 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('wp_archives_'.$lastpost); if(emptyempty($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 . ' &nbsp;(' . count($postresults) . '&nbsp;篇文章)</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, &quot;' . wp_specialchars($text, 1) . '&quot;'; $output .= '<li>' . mysql2date('d日', $postresult->post_date) . ':&nbsp;' . "<a href='$url' title='$title_text'>$text</a>"; $output .= '&nbsp;(' . $postresult->comment_count . ')'; $output .= '</li>' . "n"; } } } $output .= '</ul></li></ul>' . "n"; } update_option('wp_archives_'.$lastpost,$output); }else{ $output = '<div class="errorbox">Sorry, no posts matched your criteria.</div>' . "n"; } } echo $output; } [/php] 2. 复制一份主题的 page.php 更名为 archives.php,然后在最顶端加入(有这个文件的可以略去这一步): [php] <?php /* Template Name: archives */ ?> [/php] 另外把里面的评论板块去掉。 再然后找到类似 ,在其下面加入如下代码 [php] <a id="expand_collapse" href="#">全部展开/收缩</a> <div id="archives"><?php wp_archives_list(); ?></div> [/php] 进wp后台添加一新页面,在右侧栏模板选择 archives 3. 如果你的主题本身加载了 jQuery 库,那么继续把下面的 jQ 代码加上去,就会有滑动伸缩效果了 [php] /* 存档页面 jQ伸缩 */ $('#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'); }); [/php] 最终效果见本站归档页!

站点声明:部分内容源自互联网,为传播信息之用,如有侵权,请联系我们删除。

© Copyright 2011-2024 www.kfju.com. All Rights Reserved.
超级字帖 版权所有。蜀ICP备12031064号