WordPress免插件纯代码快速实现文章归档页的方法

发表时间:2016-01-16 23:00 | 分类:建站经验 | 浏览:3,095 次

自从本博客开始写到现在也有一段时间,之前博主一直用wordpress自带小工具中的归档文章。现在发现有个问题,默认wordpress归档工具是按照年月展示,如果博客建站时间久了之后归档列表就会拉很长,放哪里都不美观。所以现在是该把文章归档好好整理下。我觉得最简单的办法就是新建一个页面,里面只显示归档文章,包括标题、发表时间和链接等信息。添加的功能最好全自动化,我可不想因为某个功能而去多操作一步。这也是博主的原则和基本要求。

思路有两个,一是直接取wordpress数据库的文章,然后统计。这个办法比较简单,不过如果文章量比较大,可能会造成归档页打开缓慢。另外就是增加或者删除文章时,在数据库新增一个表或者一行记录,把归档文章信息保存到那里。然后前台页面直接读一次数据,不需要遍历wordpress文章表。这个办法就是前台读取速度会比较快,但是增加删除文章时可能会卡下。

这里博主介绍的是第二种,具体效果如:https://zhangnq.com/archives 。

1、模板函数添加

打开wordpress主题文件夹下的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' target='_blank'>$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;
}

添加后保存。这行代码会在你的wp-options表中添加一个类似SHe_archives_xxx的记录。

2、制作页面模板

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

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

再找到类似 <?php content(); ?>,在其下面加入如下代码:

<div id="archives"><?php archives_list_SHe(); ?></div>

3、新建archive归档页

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

到这一步,免插件纯代码实现WordPress文章的归档页面就算完成了。

如果你对一种方法感觉,可以参考wordpress钩子template_redirect来实现,具体可以参考博主前面介绍的文章《使用template_redirect为wordpress添加自定义页面》。

参考链接:http://zww.me/archives/25209

本文标签:

本文链接:https://www.sijitao.net/2354.html

欢迎您在本博客中留下评论,如需转载原创文章请注明出处,谢谢!

一键脚本 博客历程 留言联系 文章归档 网站地图 谷歌地图
Copyright © 2010-2024 章郎虫博客 All Rights Reserved.