SMERS ~ 05012008
答应过 Zx 发一篇关于我现在 Archive List 制法的文章,那么针对 Zx 的问题,我简单说说我是怎样将按月份归档的日期向右浮动过去的
稍微看过 WordPress 程序代码的人都知道怎样添加 php 代码来显示文章存档,最简单的方法是用其自带的 get_archives 函数。这个函数还包括一些附加参数,比如输出类型(按月份、年份等等)、显示文章数等
默认的,使用下列参数:
<ul id="archive-archivebydate">
<?php get_archives('monthly', '', 'html', '', '', true); ?></ul>
会得到这样的 html 输出:
<ul id="archive-archivebydate">
<li><a href='http://localhost/2006/10/' title='October 2006'>October 2006</a> (5)</li>
<li><a href='http://localhost/2006/09/' title='September 2006'>September 2006</a> (6)</li>
<li><a href='http://localhost/2006/08/' title='August 2006'>August 2006</a> (7)</li>
</ul>
我们知道,要想让后面的文章数向右浮动,必须使 ([num]) 套在一个容器内,那么我们搜索函数 get_archives,找到它,它位于 /wp-includes/template-functions-general.php 中:
[...]
if ( 'monthly' == $type ) {
$arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts WHERE post_date < '$now' AND post_date != '0000-00-00 00:00:00' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC" . $limit);
if ( $arcresults ) {
$afterafter = $after;
foreach ( $arcresults as $arcresult ) {
$url = get_month_link($arcresult->year, $arcresult->month);
if ( $show_post_count ) {
$text = sprintf('%s %d', $month[zeroise($arcresult->month,2)], $arcresult->year);
$after = ' ('.$arcresult->posts.')' . $afterafter;
} else {
$text = sprintf('%s %d', $month[zeroise($arcresult->month,2)], $arcresult->year);
}
echo get_archives_link($url, $text, $format, $before, $after);
}
}
}
[...]
从上面代码我们可以看到,当 show_post_count 为 ture 的时候它的输出格式是这样的:
$after = ' ('.$arcresult->posts.')' . $afterafter;
那么,我们需要把它套在一个 span 标签内,通过给 span 定义向右浮动,来实现 Archive List 中的样式。所以,将上述代码改为:
$before = '<span title="'.$arcresult->posts.' posts written in this month">'.$arcresult->posts.' Posts</span>'.$afterafter;
这样,新的 html 输出就会是这样的:
<ul id="archive-archivebydate">
<li><span title="5 posts written in this month">5 Posts</span><a href="http://localhost/2006/10/" title="October 2006">October 2006</a></li>
<li><span title="6 posts written in this month">6 Posts</span><a href="http://localhost/2006/09/" title="September 2006">September 2006</a></li>
<li><span title="7 posts written in this month">7 Posts</span><a href="http://localhost/2006/08/" title="August 2006">August 2006</a></li>
</ul>
在 CSS 中增加向右浮动,完成:
#archive-archivebydate span {
float: right;
color: #85bb00;
}
以上即为更改的全过程,修改过程并不很麻烦,但是,如果每次 WordPress 升级都需要修改的话,日后进行无数次的重复性操作谁都受不了,那么,我们可以将这个函数单独分离出来,制成新的函数:
get_archives 函数整个从 template-functions-general.php 中复制到 _get_archives_plus.phpget_archives 命名为 get_archives_plus然后,打开 Archive List 页面的模板,在任意位置加入下列代码,使该页面调用 _get_archives_plus.php 文件:
<?php include_once('_get_archives_plus.php');?>
那么以后,我们就可以使用自制的新函数调用归档:
<ul id="archive-archivebydate">
<?php get_archives_plus('monthly', '', 'html', '', '', true); ?></ul>
附:_get_archives_plus.php,代码较长,直接提供下载:
下载地址:http://lib.sparanoid.com/download/_get_archives_plus.phps
11252006 / WordPress / TB / Commt
很好的分析资料,但我的Archives不知道为什么,显示不出了,好郁闷哦!!
谢谢啊写的太棒了!我收藏一下 仔细研究研究
写代码真是一件有趣的工作。
,好久没来,发现你些的我越来越看不懂了。。。好深奥那
如果没错的话,我记得有现成的这样的插件把,输出的归档样式都很不错的...用不着这么麻烦
你新建一个 Archive 页面,在左侧的 Page Template: 里选择当前主题的 Archive 模板就行了,,你可以用原版 K2 模板试试。不过有很多一部分的主题没提供 Archive 模板的
我这么做是为了把这个功能集成到现在的主题上,插件并不是解决问题根源的方法,我认为少用为妙
有时候很容易上瘾的……
恩,K2模版是开刀的好东西,我的问题是模仿/移植模版时自己做的Archives模版显示不出,当然,做cnmaxthon模版到可以显示[学习s最后不加标点]
hi, i got this page by searching google, and i think this post is very useful but i don't learn any about Chinese :( can i have a English version? thanks!
now I'm in hard time and have no time to translate it, and if you just want to get the main idea of it, I think you can try this:
http://www.google.com/language_tools?hl=en