What archive.php does (and all its related templates) is show posts based on a select criteria. A date range, or posts by a certain author, a category, or a tag. So, basically, it’s a lot like index.php.

Our theme will have one multipurpose archive.php template to cover date, category, author, and tag archives.

If you want to use a separate template for each type of archive, then you can create separate template files for them, for example:category.php, author.php, and tag.php. If WordPress doesn’t find a specific template file for each type of archive, it will default toarchive.php.

The Archive Template

Here’s what our multipurpose archive template will do:

  1. Call the_post()
  2. Check to see what kind of archive this is (date, category, author, or tag)
  3. Produce an appropriate title and, if it’s a category or tag archive, display the category or tag description (if they’ve been filled out in the Dashboard)
  4. Rewind the posts with rewind_posts()
  5. Do the usual loopy WordPress stuff

Without further ado, let’s get coding. Open archive.php and add the following to it.

<?php
/**
* The template for displaying Archive pages.
*
* Learn more: http://codex.wordpress.org/Template_Hierarchy
*
* @package Shape
* @since Shape 1.0
*/
get_header(); ?>
<section id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="page-title">
<?php
if ( is_category() ) {
printf( __( 'Category Archives: %s', 'shape' ), '<span>' . single_cat_title( '', false ) . '</span>' );
} elseif ( is_tag() ) {
printf( __( 'Tag Archives: %s', 'shape' ), '<span>' . single_tag_title( '', false ) . '</span>' );
} elseif ( is_author() ) {
/* Queue the first post, that way we know
* what author we're dealing with (if that is the case).
*/
the_post();
printf( __( 'Author Archives: %s', 'shape' ), '<span class="vcard"><a class="url fn n" href="' . get_author_posts_url( get_the_author_meta( "ID" ) ) . '" title="' . esc_attr( get_the_author() ) . '" rel="me">' . get_the_author() . '</a></span>' );
/* Since we called the_post() above, we need to
* rewind the loop back to the beginning that way
* we can run the loop properly, in full.
*/
rewind_posts();
} elseif ( is_day() ) {
printf( __( 'Daily Archives: %s', 'shape' ), '<span>' . get_the_date() . '</span>' );
} elseif ( is_month() ) {
printf( __( 'Monthly Archives: %s', 'shape' ), '<span>' . get_the_date( 'F Y' ) . '</span>' );
} elseif ( is_year() ) {
printf( __( 'Yearly Archives: %s', 'shape' ), '<span>' . get_the_date( 'Y' ) . '</span>' );
} else {
_e( 'Archives', 'shape' );
}
?>
</h1>
<?php
if ( is_category() ) {
// show an optional category description
$category_description = category_description();
if ( ! empty( $category_description ) )
echo apply_filters( 'category_archive_meta', '<div class="taxonomy-description">' . $category_description . '</div>' );
} elseif ( is_tag() ) {
// show an optional tag description
$tag_description = tag_description();
if ( ! empty( $tag_description ) )
echo apply_filters( 'tag_archive_meta', '<div class="taxonomy-description">' . $tag_description . '</div>' );
}
?>
</header><!-- .page-header -->
<?php shape_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/* Include the Post-Format-specific template for the content.
* If you want to overload this in a child theme then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
?>
<?php endwhile; ?>
<?php shape_content_nav( 'nav-below' ); ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'archive' ); ?>
<?php endif; ?>
</div><!-- #content .site-content -->
</section><!-- #primary .content-area -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Looks very familiar, doesn’t it? We’re reusing several functions and templates we’ve discussed in previous lessons, such as shape_content_nav(), content.php and content-aside.php, which we call up on line 80; and no-results.php, which we call on line 89.

The comments in the code should help you get an idea of what the archive template is doing, but in the next few sections, let’s walk through some of the “archivey” things.

Dynamic archive title

Lines 19 through 53 above are responsible for printing out the title of our archive page. How the title appears depends on which type of archive we’re viewing. Here’s the code again (you don’t have to paste anything since this is copied from above. Just examine and learn):

<h1 class="page-title">
<?php
if (is_category ()) {
printf ( __ ( 'Category Archives: %s', 'shape' ), '<span>' . single_cat_title ( '', false ) . '</span>' );
} elseif (is_tag ()) {
printf ( __ ( 'Tag Archives: %s', 'shape' ), '<span>' . single_tag_title ( '', false ) . '</span>' );
} elseif (is_author ()) {
/*
* Queue the first post, that way we know what author we're dealing with (if
* that is the case).
*/
the_post ();
printf ( __ ( 'Author Archives: %s', 'shape' ), '<span class="vcard"><a class="url fn n" href="' . get_author_posts_url ( get_the_author_meta ( "ID" ) ) . '" title="' . esc_attr ( get_the_author () ) . '" rel="me">' . get_the_author () . '</a></span>' );
/*
* Since we called the_post() above, we need to rewind the loop back to the
* beginning that way we can run the loop properly, in full.
*/
rewind_posts ();
} elseif (is_day ()) {
printf ( __ ( 'Daily Archives: %s', 'shape' ), '<span>' . get_the_date () . '</span>' );
} elseif (is_month ()) {
printf ( __ ( 'Monthly Archives: %s', 'shape' ), '<span>' . get_the_date ( 'F Y' ) . '</span>' );
} elseif (is_year ()) {
printf ( __ ( 'Yearly Archives: %s', 'shape' ), '<span>' . get_the_date ( 'Y' ) . '</span>' );
} else {
_e ( 'Archives', 'shape' );
}
?>
</h1>

You can see that the title will be wrapped inside an <h1> tag. What falls inside the h1 tags depends on the type of archive.

If it’s a category archive, the title will look like this:

Category Archives: Category Name

If we’re dealing with a tag archive, we’ll get this for a title:

Tag Archives: Tag Name

For an author archive, this will be our title:

Author Archives: Author Name

Our day-based archive will produce the following title:

Daily Archives: Date (the date appears in the format you set in Dashboard > Settings > General)

Monthly archive titles follow this pattern:

Monthly Archives: November 2012

Yearly archive titles look like this:

Yearly Archives: 2012

Finally, if this is an archive, but none of the cases mentioned above (e.g. not a category, tag, author, or date archive), then the title is simply:

Archives

Category and tag descriptions

So, we’ve taken care of the title. Now, look at lines 54 through 67 inarchive.php. I’ll repeat it here for your convenience.

<?php
if (is_category ()) {
// show an optional category description
$category_description = category_description ();
if (! empty ( $category_description ))
echo apply_filters ( 'category_archive_meta', '<div class="taxonomy-description">' . $category_description . '</div>' );
} elseif (is_tag ()) {
// show an optional tag description
$tag_description = tag_description ();
if (! empty ( $tag_description ))
echo apply_filters ( 'tag_archive_meta', '<div class="taxonomy-description">' . $tag_description . '</div>' );
}
?>

WordPress lets you enter descriptions for categories and tags. This section of code checks each category or tag for a description. If it finds one, it’ll display it just below the archive title (and just before the first post in the archive listing), wrapped in a div with the class “taxonomy-description”.

If you would rather not have this feature, then all you have to do is delete the above block of code from archive.php.

All right, that’s it. The rest of the archive template is the same goodness we’ve seen in index.php and single.php.

WordPress 主题开发 - (十三) Archive模板 待翻译的更多相关文章

  1. WordPress 主题开发 - (八) Head模板 待翻译

    THE WORDPRESS THEME HEADER TEMPLATE Now we get into the nitty-gritty: building up your header.php an ...

  2. 黄聪:《跟黄聪学WordPress主题开发》

    又一个作品完成!<跟黄聪学Wordpress主题开发>,国内最好的Wordpress主题模版开发视频教程!! 目录预览: WordPress官方源文件层式结构讲解 WordPress数据库 ...

  3. wordpress 主题开发

    https://yusi123.com/3205.html https://themeshaper.com/2012/10/22/the-themeshaper-wordpress-theme-tut ...

  4. [转]WordPress 主题教程 #2:模板文件和模板

    本文转自:http://blog.wpjam.com/m/wp-theme-lesson-2-template-files-and-templates/ 模板文件(template files)和模板 ...

  5. WordPress 主题开发 - (三) 开发工具 待翻译

    Before we get started building any WordPress Theme, we’re going to need to get our development tools ...

  6. WordPress 主题开发 - (一) 前言 待翻译

    原文出自: http://themeshaper.com/2012/10/22/the-themeshaper-wordpress-theme-tutorial-2nd-edition/ THE TH ...

  7. [转]WordPress主题开发:主题初始化

    本文转自:http://www.cnblogs.com/tinyphp/p/4391182.html 在最简单的情况下,一个WordPress主题由两个文件构成: index.php -------- ...

  8. WordPress主题开发:主题初始化

    在最简单的情况下,一个WordPress主题由两个文件构成: index.php ------------------主模版 style.css  -------------------主样式表(注意 ...

  9. WordPress 主题开发:从入门到精通(必读)

    本专栏介绍如何开发设计你自己的 WordPress 主题.如果你希望了解更多如何安装和应用主题的内容,请参阅应用主题文档.本文的内容不同于应用主题,因为所讨论的是编写代码去构建你自己的主题的技术内容, ...

随机推荐

  1. A - Red and Black(3.2.1)(小递归)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  2. 避免多层回调,Node.js异步库Async使用(parallel)

    原来写的一个分页查询,回调了好几层. exports.list = function(req,res) { var params = {}; var current_page = common_uti ...

  3. hive 调用java的函数和科学记数法转换

    hive中field如果是string,比如id,那么使用10000000000+id结果是科学计数法显示,转换为正常显示方法: select (10000000000+cast(id as int) ...

  4. IOS开发-jqeurey mobile

    有一阵子没写东西了,最近打算重新拾起开发,做点手机上的东东,选中了phonegap.jquery mobile,phonegap的部署网上非常多,有空了再班门弄斧,这里先记下jquery mobile ...

  5. oracle 表空间管理

    1.创建表空间 03:01:55 sys@ORADB11G> create tablespace TSPITR datafile '/u01/app/oracle/oradata/TSPITR0 ...

  6. cocos2d-lua 3.5 ios搭建步骤

    xcode搭建cocos2d-lua是最简单的,不用 配置一系列环境变量,只把xcode安装好就可以 步骤一:去官网下载quick-3.5,然后打开命令行工具 步骤二:cd进入/Users/song/ ...

  7. 如何在低api中使用View的属性设置方法如setAlpha等

    ViewPagerTransforms 是一个自定义了各种翻转效果的开源库,其中的各种PageTransformer使用了view的很多属性设置方法如DepthPageTransformer中:? p ...

  8. CentOS 6.5 Maven 编译 Apache Tez 0.8.3 踩坑/报错解决记录

    最近准备学习使用Tez,因此从官网下载了最新的Tez 0.8.3源码,按照安装教程编译使用.平时使用的集群环境是离线的,本打算这一次也进行离线编译,无奈一编译就开始报缺少jar包的错,即使手动下载ja ...

  9. 慕课网-安卓工程师初养成-3-4 Java中的比较运算符

    来源:http://www.imooc.com/code/1299 比较运算符用于判断两个数据的大小,例如:大于.等于.不等于.比较的结果是一个布尔值( true 或 false ). Java 中常 ...

  10. ORA-12518,TNS:listener could not hand off client connection

    前几天在启动应用的时候,在控制台抛出了此异常信息!很明显是数据库方面的问题,不过具体是什么问题哪?百度了一下,网上关于此问题的信息还是有比较多,从异常的提示中我们也能看到是具体是和客户端的连接相关的问 ...