- WordPress 2.7 Cookbook
- Jean-Baptiste Jung
- 497字
- 2021-04-01 13:55:51
Creating an archive page
More and more blogs feature an archive page where they display all of their posts. There are two good reasons to do it—the first is SEO and second is your visitors. With an archive page, any search engine crawler is able to easily index all your posts. When I find a blog of my choice, I'm used to browsing through their archive page to get a quick view of what might interest me.
In this recipe, you shall learn how to create an useful archive page for your WordPress theme, by using the page template technique.
Getting ready
As I just said, we shall be using a page template to create an archive page, for which you have to understand the page template concept—explained in the previous recipe.
I'm using the WordPress default theme in the following example, so you might have to adapt the HTML markup a bit to make it fit your own theme.
How to do it...
- Create a file named
archive-custom.php
on your WordPress theme directory. - Enter the following code in that file:
<?php /* Template Name: Custom Archives */ ?> <?php get_header(); ?> <div id="content" class="narrowcolumn"> <?php while(have_posts()) : the_post(); ?> <h2><?php the_title(); ?></h2> <ul id="archive-list"> <?php $myposts = get_posts('numberposts=-1&offset=0'); foreach($myposts as $post) : ?> <li><?php the_time('m/d/y') ?>: <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> <?php endwhile; ?> </div> <?php get_sidebar(); ?> <?php get_footer(); ?>
- Save the file and upload it to the
wp-content/themes/yourtheme
directory of your WordPress install. - Log in to your WordPress Dashboard, create a new page, and select Custom Archives as a page template. Give it a title of your choice, for example Archives.
- Publish the page. Your theme now features an archive page; cool for both, search engines and visitors!
How it works...
Basically, an archive page is just a page which loops through all posts you wrote, and only display the post title and its date.
In the example, we have used the get_posts()
function instead of the classic WordPress loop. The reason behind this is that the WordPress loop's purpose is to display posts on your blog homepage depending on certain parameters, such as posts per page or pagination information. However, on our archive page we only need to display all post titles on a single page.
You must have noticed that the get_posts()
function have two parameters:
- The first one is
numberposts
, which is used to specify the number of posts you want to retrieve. I have set it up at -1, which means that I want to get all the available posts. - The second parameter used is
offset
; which allow you to specify the number of posts that you don't want to be displayed. If you specifyoffset=5
, the first post you'll get will be the sixth. Theoffset=0
in the example doesn't change anything at all. For example, you should enhance the archive page we just created by creating a first loop which will only get you the most recent posts and style it differently.
There's more...
For those who prefer to use a plugin instead of creating their own archive page, I have used the Smart Archives plugin some time ago and enjoyed it. You can get it for free at the following link, http://justinblanton.com/projects/smartarchives/.
- 從零開始:AutoCAD 2015中文版機械制圖基礎培訓教程
- Photoshop CS5平面設計入門與提高
- Photoshop案例實戰從入門到精通
- UG NX 9.0中文版 基礎教程 (UG工程師成才之路)
- AIGC輔助數據分析與挖掘:基于ChatGPT的方法與實踐
- 品悟:Alias 2014+Showcase產品可視化造型與渲染
- ChronoForms 3.1 for Joomla! site Cookbook
- 影視動畫場景與特效制作:3ds Max-Vue-AfterBurn-FumeFX
- PyTorch深度學習簡明實戰
- Photoshop CS6實戰基礎培訓教程(全視頻微課版)
- 學摳圖:Photoshop專業摳圖技法案例教程
- Premiere Pro短視頻剪輯零基礎一本通
- 新編 中文版Photoshop平面設計入門與提高
- 中文版AutoCAD 2022從入門到精通
- 24小時學會Word-Excel-PowerPoint 2010三合一