- 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/.
- 3ds Max/VRay室內設計材質、燈光與建模速查超級手冊
- 魔法詞典:AI繪畫關鍵詞圖鑒(Stable Diffusion版)
- DSP開發寶典
- Cinema 4D完全實戰技術手冊
- Instant MuseScore
- Microsoft Dynamics GP 2010 Reporting
- 說服力:工作型PPT該這樣做(第2版)
- Designing and Implementing Linux Firewalls and QoS using netfilter, iproute2, NAT and l7/filter
- Moldflow 2021模流分析從入門到精通(升級版)
- Building Websites with PHP/Nuke
- UG NX 12.0中文版從入門到精通
- 行攝 Photoshop CC后期修片高手之道(第2版)
- 中文版CorelDRAW X7技術大全
- Joomla! 1.5 JavaScript jQuery
- 傳奇:ZBrush數字雕刻大師之路(第2版)