WordPress免插件仅代码实现文章归档(模板页面)I

许多博友喜欢为自己的博客建立一个文章归档页面,就如新浪博客的博文归档一样。WordPress 可以用插件来实现,但我们一贯遵守可以不用插件则不用的准则,现在Jeff 就为大家带来免插件仅代码实现文章归档的方法。效果见Jeff的阳台存档页

关于WordPress 模板是什么自己去谷歌一下吧,注意不要将WordPress 主题跟模板混淆哦!

WordPress 免插件仅代码实现文章归档,coding!

新建一txt文件,打开输入如下代码:

<div class="archives">
	<?php
    $previous_year = $year = 0;
    $previous_month = $month = 0;
    $ul_open = false;
 
    $myposts = get_posts('numberposts=-1&orderby=post_date&order=DESC');
 
    foreach($myposts as $post) :
        setup_postdata($post);
 
        $year = mysql2date('Y', $post->post_date);
        $month = mysql2date('n', $post->post_date);
        $day = mysql2date('j', $post->post_date);
 
        if($year != $previous_year || $month != $previous_month) :
            if($ul_open == true) : 
                echo '</table>';
            endif;
 
            echo '<h3>'; echo the_time('F Y'); echo '</h3>';
            echo '<table>';
            $ul_open = true;
 
        endif;
 
        $previous_year = $year; $previous_month = $month;
    ?>
        <tr>
            <td width="40" style="text-align:right;"><?php the_time('j'); ?>日</td>
            <td width="400"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></td>
            <td width="120"><a class="comm" href="<?php comments_link(); ?>" title="查看 <?php the_title(); ?> 的评论"><?php comments_number('0', '1', '%'); ?>人评论</a></td>
            <td width="120"><span class="view"><?php if(function_exists('the_views')) the_views(); ?>次浏览</span></td>
        </tr>
    <?php endforeach; ?>
    </table>
</div>

在主题目录下的style.css中进入以下代码:

.archives td{padding: 6px 10px 8px;border-bottom: solid 1px #eee}
.archives table{padding:10px 0 20px}
.meta-tit{border-bottom: solid 1px #e6e6e6;padding: 0 0 10px;margin-bottom: 20px}

保存,将txt文件重命名为archives.php,注意不是archives.php.txt哦!然后在wordpress后台,页面-新建页面,题目任取,模板选择“存档”就可以实现了!

注意:

  • 上面代码中的<div id="main">等需要改为符合你的主题的选择器
  • css样式需要自己修改以符合主题整体样式;
  • 如无特别情况,不建议使用该方法,更好的方法见下面的“建议”;

建议:

就Jeff 实践,该方法有很大的局限性,且是即时输出文章会,一旦文章过多,速度会有影响。不建议使用。更好的方法见下文更新。

评分:
当前平均分 5.00 (95%) - 1 个投票
6 条 评论
  1. 按照你的方法弄成功了。如果文章越来越来多的时候要怎么分页啊?

    7年前 回复
  2. 不能分页,太长了~

    10年前 回复
  3. 这个可以吗 一直在找

    10年前 回复
  4. 为什么我这样做了,在选择新建页面时,无法选择到这个模版呢?

    10年前 回复
    • 要将php文件放到主题根目录下

      10年前 回复
      • 我放了啊,但是还是不行啊!不知道为什么?

        10年前 回复
发表评论