答应过 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>&nbsp;(5)</li>
<li><a href='http://localhost/2006/09/' title='September 2006'>September 2006</a>&nbsp;(6)</li>
<li><a href='http://localhost/2006/08/' title='August 2006'>August 2006</a>&nbsp;(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 = '&nbsp;('.$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 = '&nbsp;('.$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_plus.php 的文件
  • 然后,将修改后的 get_archives 函数整个从 template-functions-general.php 中复制到 _get_archives_plus.php
  • 最后,将函数重新命名,我把 get_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,代码较长,直接提供下载:

Download / _get_archives_plus.phps / 5.13 KB


« older in this category

newer in this category »