Custom archives page is a great way to bring together all your old content in one page. It allows you to have a dedicated page where you can list your monthly archives, category archives, tag archives, author archives, and anything else that you might want to add. We have a custom archives page for WPBeginner. Recently one of our users suggested that we write a tutorial on that. In this article, we will show you how to create a custom archives page in WordPress.

自定义档案页面是一种将所有旧内容整合到一个页面中的好方法。 它允许您有一个专用页面,您可以在其中列出您的每月档案,类别档案,标签档案,作者档案以及其他可能要添加的内容。 我们为WPBeginner提供了一个自定义档案页面 。 最近,我们的一位用户建议我们为此编写一个教程。 在本文中,我们将向您展示如何在WordPress中创建自定义档案页面。

问题 (The Problem)

By default, you have the ability to display your yearly archives by using sidebar widgets. This can get out of hands once you have been blogging for more than a year. Imagine sites like WPBeginner that have been active for nearly 4 years. We would have 48 monthly archive links in the sidebar. You also have the ability to add category archives, author listing, etc on your sidebar. But why clutter the sidebar with so many links when you can create a single page to list them all. This is when a custom archives page come in play. Not only does it allow you to avoid clutter, it also provides your users with an efficient way to browser through your older content.

默认情况下,您可以使用侧边栏小部件显示年度存档。 一旦您撰写博客超过一年,这可能会变得一发不可收拾。 想象一下像WPBeginner这样活跃了近4年的网站。 边栏中将有48个每月的归档链接。 您还可以在边栏上添加类别档案,作者列表等。 但是,当您可以创建一个页面以列出所有链接时,为什么还要在侧边栏上放置如此多的链接呢? 这是自定义存档页面开始播放的时间。 它不仅可以避免混乱,而且还为用户提供了一种浏览旧内容的有效方法。

Sadly, most WordPress themes do not come with a custom archives page template. Let’s look at what you need to create a custom archives page template in WordPress.

可悲的是,大多数WordPress主题都没有自定义档案页面模板。 让我们看看在WordPress中创建自定义档案页面模板所需要的内容。

Note: Please do not confuse the custom archives with archive.php template that comes with most WordPress themes. The archive.php template is used to display monthly, category, tag, author, and other archive pages. Our custom archives page would be a single page that will bring all of your other archives together.

注意:请勿将自定义档案与大多数WordPress主题随附的archive.php模板混淆。 archive.php模板用于显示每月,类别,标签,作者和其他存档页面。 我们的自定义档案页面将是一个页面,它将所有其他档案汇集在一起​​。

创建自定义档案页面模板 (Creating a Custom Archives Page Template)

First thing you need to do is to create a page template for the custom archives page. Simply open a new file in your text editor (i.e Notepad) and name it page-archive.php. Next, Add the following lines of code at the top:

您需要做的第一件事是为自定义档案页面创建页面模板 。 只需在文本编辑器(即记事本)中打开一个新文件,并将其命名为page-archive.php即可 。 接下来,在顶部添加以下代码行:

Template Name: Archives

Upload the page-archive.php in your WordPress themes folder, and you have created an Archives page template. Now we need to make sure that this page template matches the design of your site. Copy the content of your page.php file located in your theme’s folder and paste it in page-archive.php.

在您的WordPress主题文件夹中上传page-archive.php ,您已经创建了一个Archives页面模板。 现在,我们需要确保此页面模板与您的网站设计匹配。 复制位于主题文件夹中的page.php文件的内容,并将其粘贴到page-archive.php

Below is an example of how your page-archive.php file would look like:


Template Name: Archives
get_header(); ?>

<div id="primary" class="site-content">
<div id="content" role="main">

<?php while ( have_posts() ) : the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>

<div class="entry-content">

<?php the_content(); ?>

/* Custom Archives Functions Go Below this line */

/* Custom Archives Functions Go Above this line */

</div><!-- .entry-content -->

<?php endwhile; // end of the loop. ?>

</div><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

在WordPress中创建自定义档案页面 (Creating a Custom Archives Page in WordPress)

Now that you have the basic page template ready, you need to create a new custom archives page in WordPress. Go to your WordPress admin panel and add a new page (Pages » New). You can call this page Archives, Library, or anything else that you like. Now look at the meta boxes below the publish button on the right hand side of your screen. You should see a meta box called Page Attributes. Click on the drop down menu below Template and choose Archive as your page template. Save and Publish the page.

现在您已经准备好基本页面模板,您需要在WordPress中创建一个新的自定义存档页面。 转到WordPress管理面板并添加一个新页面( Pages»New )。 您可以将此页面称为“存档”,“库”或其他任何您喜欢的东西。 现在,查看屏幕右侧“发布”按钮下方的元框。 您应该看到一个名为“页面属性”的元框。 单击“模板”下面的下拉菜单,然后选择“存档”作为页面模板。 保存并发布页面。

Select Archives Page Template in WordPress

Now you have created a page that uses the archives page template, however it will not show any content. Let’s go ahead and add custom archive page elements such as yearly archives, categories, etc.

现在,您已经创建了一个使用档案页面模板的页面,但是该页面不会显示任何内容。 让我们继续添加自定义存档页面元素,例如年度存档,类别等。

使用压缩档案添加每月档案 (Adding Monthly Archives with Compact Archives)

If you look at our custom archives page, then you will notice that we are not using the default monthly archives listing that comes with WordPress. Instead, we are using a plugin called Compact Archives. Note we have adopted this plugin and are now maintaining it.

如果您查看我们的自定义档案页面 ,那么您会注意到我们没有使用WordPress随附的默认每月档案列表。 相反,我们使用的是名为Compact Archives的插件。 请注意,我们已经采用了此插件,并且正在对其进行维护。

Install and activate this plugin the Compact Archives plugin. After activating the plugin, add the following code in your custom archives page template (page-archive.php):

安装并激活该插件Compact Archives插件。 激活插件后,在自定义档案页面模板(page-archive.php)中添加以下代码:

<p><strong>By Date</strong></p>
<?php compact_archive($style='block'); ?>

It will display your monthly archives like this:


Displaying monthly archives one year per row using Compact Archives

添加所有类别的列表 (Adding a List of all Categories)

Categories summarize the main topics of your website and are the best way to sort your content. See why how we use Categories vs Tags. Since we are using categories as the main way to organize our content, we think it is absolutely crucial to list our category archives. To save space, we are going to display it an inline list.

类别概述了网站的主要主题,并且是对内容进行排序的最佳方法。 了解为什么我们使用类别vs标签 。 由于我们使用类别作为组织内容的主要方式,因此我们认为列出类别档案绝对至关重要。 为了节省空间,我们将向其显示一个内联列表。

First add this code in your archives page template file:


<ul class="bycategories">
<?php wp_list_categories('title_li='); ?>
<div class="clear"></div>

Now we need to style this list, make it appear inline and improve their look. Add this to your theme’s style.css file:

现在,我们需要对该列表进行样式设置,使其内联并改善其外观。 将此添加到主题的style.css文件中:

ul.bycategories {
margin: 0;
padding: 0;
ul.bycategories li {
list-style: none; 
list-style-type: none;
margin: 0; 
padding: 0;
ul.bycategories li a {
list-style: none; 
list-style-type: none;
margin: 0 20px 15px 0; 
float: left; 
background: #eee; 
color: #464646; 
padding: 5px 10px;
border-radius: 5px; 
-moz-border-radius: 5px; 
-webkit-border-radius: 5px;
ul.bycategories li a:hover{
text-decoration: none; 
background: #ff6200; 
color: #fff;
.clear{clear: both;}

Your categories will look like this:


Displaying in line categories on archives page in WordPress

探索? 将用户重定向到随机帖子 (Explore? Redirect Users to a Random Post)

In our archives page, we have an Explore WPBeginner button. This button redirects users to a random post. The purpose is to allow users to randomly stumble through articles. Learn how to redirect users to a random post in WordPress.

在存档页面中,我们有一个Explore WPBeginner按钮。 此按钮将用户重定向到随机帖子。 目的是允许用户随机浏览文章。 了解如何将用户重定向到 WordPress中的随机帖子

While this is all the information that we have on our custom archives page, you can most certainly add more. Let’s look at some of the other things that you can add.

尽管这是我们自定义档案页面上拥有的所有信息,但是您可以肯定可以添加更多信息。 让我们看看您可以添加的其他一些内容。

添加标签云 (Adding a Tag Cloud)

If you want to display a tag cloud of your most popular tags used on the site, then simply add the following code in custom-archive.php file:


<p><strong>Tags Cloud:</strong></p>
<?php wp_tag_cloud(); ?>

The wp_tag_cloud() function comes with a lot of parameters to adjust the number of tags, maximum and minimum tag sizes, etc.


添加页面列表 (Adding a List of Pages)

If you want to display a list of all pages on your site, then simply add the following code:


<?php wp_list_pages( 'title_li=' ); ?>

添加作者列表 (Adding a List of Authors)

To display the list of authors on the site, simply add the following code:


<?php wp_list_authors( 'exclude_admin=0&optioncount=1' ); ?>

添加最近的帖子 (Adding Recent Posts)

If you want to display a list of your most recent posts, then add this code:


<?php wp_get_archives('type=postbypost&limit=10'); ?>

A comprehensive archives page allows your users to efficiently navigate through your old content. We hope that this article helped you create a custom archives page in WordPress. If you have any questions or suggestions, then please let us know by leaving a comment below.

全面的存档页面使您的用户可以有效地浏览您的旧内容。 我们希望本文能帮助您在WordPress中创建自定义档案页面。 如果您有任何疑问或建议,请在下面留下评论,让我们知道。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-create-an-archives-page-in-wordpress/

  • 0
  • 0
  • 0
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
钱包余额 0