前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WordPress免插件仅代码实现文章归档模板 II

WordPress免插件仅代码实现文章归档模板 II

作者头像
Jeff
发布2018-01-19 11:20:43
2.2K0
发布2018-01-19 11:20:43
举报
文章被收录于专栏:DeveWorkDeveWork

不多说了,本站目前使用的效果,可以到我的存档页一览,该方法来自zww,感谢原作者,在这之前,建议你:

1、你的博客存档页面地址后面的应该为/articles,而不是常见的/archives 。在英文中,article表示“文章”,而archive意为“档案”,使用archive貌似不那么规范;2、你的博客存档页面应该禁止搜索引擎访问,因为这会造成内容重复(在你启用站点地图插件的情况下)。

折腾功能:代码实现WordPress归档页面模板[WP原生函数篇]

原创作者:zwwooooo

特点:

1. 按照年份、月份显示文章列表 2. 显示每月的文章数量(需要配合及Query) 3. 显示每篇文章的评论数 4. 使用 WordPress 原生函数实现数据调用 5. 这个存档函数会在数据库生成一个表 zww_archives_list 来做缓存,只在发表/修改文章时才更新,减少数据库查询。 6. 即使不使用第5点的数据库缓存功能也比以前的直接 SQL 语句省资源。

步骤:

1. 把下面的函数扔到所用主题的 functions.php 文件里面:(注意:因为有中文,所以要把 functions.php 文件转换为 UTF8 无 BOM 格式,不然中文会乱码)。

/* Archives list by zwwooooo | http://zww.me */ function zww_archives_list() { if( !$output = get_option('zww_archives_list') ){ $output = '<div id="archives"><p>[<a id="al_expand_collapse" href="#">全部展开/收缩</a>] <em>(注: 点击月份可以展开)</em></p>'; $the_query = new WP_Query( 'posts_per_page=-1&ignore_sticky_posts=1' ); //update: 加上忽略置顶文章 $year=0; $mon=0; $i=0; $j=0; while ( $the_query->have_posts() ) : $the_query->the_post(); $year_tmp = get_the_time('Y'); $mon_tmp = get_the_time('m'); $y=$year; $m=$mon; if ($mon != $mon_tmp && $mon > 0) $output .= '</ul></li>'; if ($year != $year_tmp && $year > 0) $output .= '</ul>'; if ($year != $year_tmp) { $year = $year_tmp; $output .= '<h3 class="al_year">'. $year .' 年</h3><ul class="al_mon_list">'; //输出年份 } if ($mon != $mon_tmp) { $mon = $mon_tmp; $output .= '<li><span class="al_mon">'. $mon .' 月</span><ul class="al_post_list">'; //输出月份 } $output .= '<li>'. get_the_time('d日: ') .'<a href="'. get_permalink() .'">'. get_the_title() .'</a> <em>('. get_comments_number('0', '1', '%') .')</em></li>'; //输出文章日期和标题 endwhile; wp_reset_postdata(); $output .= '</ul></li></ul></div>'; update_option('zww_archives_list', $output); } echo $output; } function clear_zal_cache() { update_option('zww_archives_list', ''); // 清空 zww_archives_list } add_action('save_post', 'clear_zal_cache'); // 新发表文章/修改文章时

2. 复制一份主题的 page.php 更名为 archives.php,然后在最顶端加入:

<?php /* Template Name: archives */ ?>

然后找到类似 ,在其下面加入如下代码

<?php zww_archives_list(); ?>

进wp后台添加一新页面,在右侧栏模板选择 archives

3. 给主题加载 jQuery 库。

4. jQuery 效果代码

jQuery(document).ready(function($){ //===================================存档页面 jQ伸缩 (function(){ $('#al_expand_collapse,#archives span.al_mon').css({cursor:"s-resize"}); $('#archives span.al_mon').each(function(){ var num=$(this).next().children('li').size(); var text=$(this).text(); $(this).html(text+'<em> ( '+num+' 篇文章 )</em>'); }); var $al_post_list=$('#archives ul.al_post_list'), $al_post_list_f=$('#archives ul.al_post_list:first'); $al_post_list.hide(1,function(){ $al_post_list_f.show(); }); $('#archives span.al_mon').click(function(){ $(this).next().slideToggle(400); return false; }); $('#al_expand_collapse').toggle(function(){ $al_post_list.show(); },function(){ $al_post_list.hide(); }); })(); });

PS:不知道怎么写js文件调用的就直接打开 header.php 并找到 ,在其下面加上 上面那段jQuery代码

4. css根据需要写,不写也可以的。HTML结构:

<div id="archives"> <p>[<a id="al_expand_collapse" href="#">全部展开/收缩</a>] <em>(注: 点击月份可以展开)</em></p> <h3 class="al_year">'年份</h3> <ul class="al_mon_list"> <li><span class="al_mon">月份<em> (本月文章数量)</em></span> <ul class="al_post_list"> <li>号数: <a href="文章链接">文章标题</a> <em>(评论数量)</em></li> </ul> </li> </ul> </div>

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013/04/03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 步骤:
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档