官术网_书友最值得收藏!

  • 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...

  1. Create a file named archive-custom.php on your WordPress theme directory.
  2. 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(); ?>
  3. Save the file and upload it to the wp-content/themes/yourtheme directory of your WordPress install.
  4. 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.
  5. 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:

  1. 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.
  2. 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 specify offset=5, the first post you'll get will be the sixth. The offset=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/.

主站蜘蛛池模板: 屏东市| 广灵县| 玉树县| 冷水江市| 那坡县| 阳新县| 汝城县| 潮安县| 玉山县| 吴桥县| 德格县| 辰溪县| 中卫市| 福泉市| 荥阳市| 南皮县| 阳新县| 泾源县| 桐城市| 奈曼旗| 崇信县| 石首市| 陇西县| 浙江省| 澜沧| 威远县| 乐清市| 漳浦县| 桑植县| 汽车| 横峰县| 镇坪县| 自贡市| 大关县| 张家川| 阳新县| 宜昌市| 镇远县| 峨山| 常州市| 闽侯县|