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

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

主站蜘蛛池模板: 武安市| 海南省| 定襄县| 桂阳县| 巍山| 长岭县| 当阳市| 鄱阳县| 盐山县| 河津市| 外汇| 岗巴县| 普格县| 双桥区| 炉霍县| 合川市| 安远县| 保靖县| 桂东县| 奈曼旗| 泽库县| 册亨县| 乐业县| 江北区| 香港 | 唐河县| 长汀县| 遵义市| 柘荣县| 新龙县| 渭源县| 漳州市| 景泰县| 浙江省| 凌云县| 平江县| 丹东市| 海林市| 兴国县| 镇平县| 永嘉县|