更好的WordPress主题

Wordpress 存档页面模板制作(代码版)

摘要:

自D5主题共享发布以来,不少同学希望能够增加存档页面,也就是站点所有文章的集合。

下面介绍一种themebetter使用的存档方式,先看代码:

<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 type="text/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}
</style>

如不喜欢以上样式可以自行修改。

其实Wordpress存档页面就是这么简单,如果你安装了wp-postviews插件,存档页面中将显示文章浏览次数,很不错吧!

本文属原创,转载请注明原文:https://themebetter.com/wordpress-page-archives.html

感觉很棒,欢迎打赏~

1823 打赏
分享到:

评论 (1)

登录后评论
注:有关主题的售前售后请 提交工单
头像
什么时候能把当月的文章归档展示,(非当月)文章归档默认收缩呀。从头往下拉太长了……

建站!你有充足的理由选择我们

世界上超过43%的网站是由WordPress搭建,而我们是国内最靠谱的WordPress主题开发商
QQ客服 微信客服themebetter微信客服 在线工单 公众号themebetter微信公众号