代码实现WordPress归档页面模板
2015-03-04 20:30
429 查看
前几天我在自力博客看到 hzlzh 写的《WordPress归档页面模板,按日期列出所有文章》,在本地测试了一下,代码量很少,但每次访问存档页面数据库查询量太大导致打开页面很慢,于是放弃。
昨天帮一朋友修改一私人模板,发现里面有个函数是生成所有文章列表,问其出处,朋友说不知道,这么好的代码为啥没共享出来呢?于是有了这篇文章。
特点:
1. 这个存档函数会在数据库生成一个表 SHe_archives_25216,用来保存在文章新发表/删除文章时生成的 html,这主要是加快访问速度,不用每次都要查询数据库生成归档。
2. 显示每月文章数
3. 显示每篇文章的评论数
效果:见我博客的存档页
说明:我另外加了jQuery滑动效果,jQ代码来自上面提到的 hzlzh,稍微修改。
下面是详细步骤
1. 把下面的 archives_list_SHe 函数代码扔进主题的 functions.php 里面 (2010.7.23
edit)
2. 复制一份主题的 page.php 更名为 archives.php,然后在最顶端加入:
再然后找到类似 <?php content(); ?>,在其下面加入如下代码(2010.7.21
edit)
进wp后台添加一新页面,在右侧栏模板选择 archives
3. 如果你的主题本身加载了 jQuery 库,那么继续把下面的 jQ 代码加上去,就会有滑动伸缩效果了(2010.7.23
edit)
css 样式可以通过 #archives 来定义
从折腾WordPress就一直使用的存档页面插件 Clean
Archives Reloaded 终于可以光荣退休了。
ok,折腾完。
2010.7.21 update:由于ie下有点问题,所以重新修改了一下代码,折腾过的再
CP 一次吧。
2010.7.23 update:ie6/7下还是有问题,所以又重新修改了一下代码,囧~这次完美了吧,折腾过的再
CP 一次吧 - -!
声明: 除非注明,ZWWoOoOo文章均为原创,转载请以链接形式标明本文地址
本文地址: http://zww.me/archives/25209
昨天帮一朋友修改一私人模板,发现里面有个函数是生成所有文章列表,问其出处,朋友说不知道,这么好的代码为啥没共享出来呢?于是有了这篇文章。
特点:
1. 这个存档函数会在数据库生成一个表 SHe_archives_25216,用来保存在文章新发表/删除文章时生成的 html,这主要是加快访问速度,不用每次都要查询数据库生成归档。
2. 显示每月文章数
3. 显示每篇文章的评论数
效果:见我博客的存档页
说明:我另外加了jQuery滑动效果,jQ代码来自上面提到的 hzlzh,稍微修改。
下面是详细步骤
1. 把下面的 archives_list_SHe 函数代码扔进主题的 functions.php 里面 (2010.7.23
edit)
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 */ ?>
再然后找到类似 <?php content(); ?>,在其下面加入如下代码(2010.7.21
edit)
<a id="expand_collapse" href="#">全部展开/收缩</a> <div id="archives"><?php archives_list_SHe(); ?></div>
进wp后台添加一新页面,在右侧栏模板选择 archives
3. 如果你的主题本身加载了 jQuery 库,那么继续把下面的 jQ 代码加上去,就会有滑动伸缩效果了(2010.7.23
edit)
/* 存档页面 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'); });
css 样式可以通过 #archives 来定义
从折腾WordPress就一直使用的存档页面插件 Clean
Archives Reloaded 终于可以光荣退休了。
ok,折腾完。
2010.7.21 update:由于ie下有点问题,所以重新修改了一下代码,折腾过的再
CP 一次吧。
2010.7.23 update:ie6/7下还是有问题,所以又重新修改了一下代码,囧~这次完美了吧,折腾过的再
CP 一次吧 - -!
声明: 除非注明,ZWWoOoOo文章均为原创,转载请以链接形式标明本文地址
本文地址: http://zww.me/archives/25209
相关文章推荐
- WordPress 单页面上一页下一页的实现方法【附代码】
- 怎么用代码制作WordPress的归档页面
- 【asp】【View】实现模板输出,页面与逻辑代码进行分离
- WordPress代码和分析-页面模板是从哪里来的
- WordPress代码和分析-页面模板是从哪里来的(这是一个可以让人奔溃的设计)
- WordPress 单页面上一页下一页的实现方法【附代码】
- WordPress代码和分析-页面模板是从哪里来的
- 代码实现wordpress博客归档数据统计
- 在后台代码里实现在aspx页面任意位置插入html代码的好办法
- 基于jQuery实现的当离开页面时出现提示的实现代码
- AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单! )(示例代码下载)
- 织梦前台模板页面实现显示发布时间格式为XX秒前、XX分钟前
- WordPress 实战:在wordpress文章中加入分享到微博及社交网站的按钮(代码实现,非插件)
- 解析PHP计算页面执行时间的实现代码
- ASP.NET返回上一页面的实现代码
- jsp中一个页面引入另一个页面的实现代码
- PHP: WordPress 之 在注册界面 实现 注册后密码直接显示在页面上
- jsp中未登录用户也可以浏览页面的功能实现代码
- eShop电子商城的SSH实现的用户模块(1)登录和注册配置文件和页面关键代码(V...