原文:The Archive, Author, Category & Tags Template
原文作者:Ian Stewart
译文作者:Young
和 index.php 模板类似,我们要创建一个母模板,并通过它来创建其他子模板。这里的母模板是指存档模板。
archive.php (以及它的相关模板)的作用是显示符合某些条件(例如某一段时间、某一个作者、某一个分类或者某一个标签等)的日志,和 index.php 很类似。如果能够理解这些模板的字面意思,您就知道这些模板是干什么用的了。
让我们从之前的教程开始,下面是此文所有模板都要用到的相同的代码:
- <?php get_header(); ?>
- <div id="container">
- <div id="content">
- <div id="nav-above" class="navigation">
- </div><!– #nav-above –>
- <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
- </div><!– #post-<?php the_ID(); ?> –>
- <div id="nav-below" class="navigation">
- </div><!– #nav-below –>
- </div><!– #content –>
- </div><!– #container –>
- <?php get_sidebar(); ?>
- <?php get_footer(); ?>
存档模板
存档模板的功能如下:
- 调用 the_post() 函数;
- 检测这个是什么模板;
- 生成一个合适的模板;
- 通过 rewind_post() 倒序排列文章;
- 执行 WordPress 循环功能。
下面是 archive.php 模板的内容,注意顶部的条件标签是用来检测目前是什么模板的:
- <?php the_post(); ?>
- <?php if ( is_day() ) : ?>
- <h1 class="page-title"><?php printf( __( ‘Daily Archives: <span>%s</span>’, ‘your-theme’ ), get_the_time(get_option(‘date_format’)) ) ?></h1>
- <?php elseif ( is_month() ) : ?>
- <h1 class="page-title"><?php printf( __( ‘Monthly Archives: <span>%s</span>’, ‘your-theme’ ), get_the_time(‘F Y’) ) ?></h1>
- <?php elseif ( is_year() ) : ?>
- <h1 class="page-title"><?php printf( __( ‘Yearly Archives: <span>%s</span>’, ‘your-theme’ ), get_the_time(‘Y’) ) ?></h1>
- <?php elseif ( isset($_GET['paged']) && !empty($_GET['paged']) ) : ?>
- <h1 class="page-title"><?php _e( ‘Blog Archives’, ‘your-theme’ ) ?></h1>
- <?php endif; ?>
- <?php rewind_posts(); ?>
- <?php global $wp_query; $total_pages = $wp_query->max_num_pages; if ( $total_pages > 1 ) { ?>
- <div id="nav-above" class="navigation">
- <div class="nav-previous"><?php next_posts_link(__( ‘<span class="meta-nav">«</span> Older posts’, ‘your-theme’ )) ?></div>
- <div class="nav-next"><?php previous_posts_link(__( ‘Newer posts <span class="meta-nav">»</span>’, ‘your-theme’ )) ?></div>
- </div><!– #nav-above –>
- <?php } ?>
- <?php while ( have_posts() ) : the_post(); ?>
- <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
- <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( __(‘Permalink to %s’, ‘your-theme’), the_title_attribute(‘echo=0′) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
- <div class="entry-meta">
- <span class="meta-prep meta-prep-author"><?php _e(‘By ‘, ‘your-theme’); ?></span>
- <span class="author vcard"><a class="url fn n" href="<?php echo get_author_link( false, $authordata->ID, $authordata->user_nicename ); ?>" title="<?php printf( __( ‘View all posts by %s’, ‘your-theme’ ), $authordata->display_name ); ?>"><?php the_author(); ?></a></span>
- <span class="meta-sep"> | </span>
- <span class="meta-prep meta-prep-entry-date"><?php _e(‘Published ‘, ‘your-theme’); ?></span>
- <span class="entry-date"><abbr class="published" title="<?php the_time(‘Y-m-d\TH:i:sO’) ?>"><?php the_time( get_option( ‘date_format’ ) ); ?></abbr></span>
- <?php edit_post_link( __( ‘Edit’, ‘your-theme’ ), "<span class=\"meta-sep\">|</span>\n\t\t\t\t\t\t<span class=\"edit-link\">", "</span>\n\t\t\t\t\t" ) ?>
- </div><!– .entry-meta –>
- <div class="entry-summary">
- <?php the_excerpt( __( ‘Continue reading <span class="meta-nav">»</span>’, ‘your-theme’ ) ); ?>
- </div><!– .entry-summary –>
- <div class="entry-utility">
- <span class="cat-links"><span class="entry-utility-prep entry-utility-prep-cat-links"><?php _e( ‘Posted in ‘, ‘your-theme’ ); ?></span><?php echo get_the_category_list(‘, ‘); ?></span>
- <span class="meta-sep"> | </span>
- <?php the_tags( ‘<span class="tag-links"><span class="entry-utility-prep entry-utility-prep-tag-links">’ . __(‘Tagged ‘, ‘your-theme’ ) . ‘</span>’, ", ", "</span>\n\t\t\t\t\t\t<span class=\"meta-sep\">|</span>\n" ) ?>
- <span class="comments-link"><?php comments_popup_link( __( ‘Leave a comment’, ‘your-theme’ ), __( ’1 Comment’, ‘your-theme’ ), __( ‘% Comments’, ‘your-theme’ ) ) ?></span>
- <?php edit_post_link( __( ‘Edit’, ‘your-theme’ ), "<span class=\"meta-sep\">|</span>\n\t\t\t\t\t\t<span class=\"edit-link\">", "</span>\n\t\t\t\t\t\n" ) ?>
- </div><!– #entry-utility –>
- </div><!– #post-<?php the_ID(); ?> –>
- <?php endwhile; ?>
- <?php global $wp_query; $total_pages = $wp_query->max_num_pages; if ( $total_pages > 1 ) { ?>
- <div id="nav-below" class="navigation">
- <div class="nav-previous"><?php next_posts_link(__( ‘<span class="meta-nav">«</span> Older posts’, ‘your-theme’ )) ?></div>
- <div class="nav-next"><?php previous_posts_link(__( ‘Newer posts <span class="meta-nav">»</span>’, ‘your-theme’ )) ?></div>
- </div><!– #nav-below –>
- <?php } ?>
作者模板
作者模板不需要改多少内容,您会喜欢这个模板的。复制 archive.php 并重命名为 author.php,然后我们所需要做的是把页面标题部分的代码替换成下面这个:
- <h1 class="page-title author"><?php printf( __( ‘Author Archives: <span class="vcard">%s</span>’, ‘your-theme’ ), "<a class=’url fn n’ href=’$authordata->user_url’ title=’$authordata->display_name’ rel=’me’>$authordata->display_name</a>" ) ?></h1>
- <?php $authordesc = $authordata->user_description; if ( !empty($authordesc) ) echo apply_filters( ‘archive_meta’, ‘<div class="archive-meta">’ . $authordesc . ‘</div>’ );?>
很简单,对吧?
分类模板
分类模板也是一个简单的模板,复制 archive.php 的所有内容并重命名为 category.php。
现在打开 functions.php。我们要添加一个自定义的功能——来自优秀的 Sandbox 主题——以便使我们的分类模板更加有用。
- // For category lists on category archives: Returns other categories except the current one (redundant)
- function cats_meow($glue) {
- $current_cat = single_cat_title( ”, false );
- $separator = "\n";
- $cats = explode( $separator, get_the_category_list($separator) );
- foreach ( $cats as $i => $str ) {
- if ( strstr( $str, ">$current_cat<" ) ) {
- unset($cats[$i]);
- break;
- }
- }
- if ( empty($cats) )
- return false;
- return trim(join( $glue, $cats ));
- } // end cats_meow
cats_meow() 函数把目前的分类从分类页面删除,换句话说,它删掉日志摘要下多余的分类目录。
现在,回到 category.php 文件,用以下代码替代页面标题部分:
- <h1 class="page-title"><?php _e( ‘Category Archives:’, ‘your-theme’ ) ?> <span><?php single_cat_title() ?></span></span></h1>
- <?php $categorydesc = category_description(); if ( !empty($categorydesc) ) echo apply_filters( ‘archive_meta’, ‘<div class="archive-meta">’ . $categorydesc . ‘</div>’ ); ?>
在 .entry-utility div 标签里找到以下代码:
- <span class="cat-links"><span class="entry-utility-prep entry-utility-prep-cat-links"><?php _e( ‘Posted in ‘, ‘your-theme’ ); ?></span><?php echo get_the_category_list(‘, ‘); ?></span><span class="meta-sep"> | </span>(原文少了最后一个 class.)
然后把上面的代码替换成下面这个:
- <?php if ( $cats_meow = cats_meow(‘, ‘) ) : // Returns categories other than the one queried ?>
- <span class="cat-links"><?php printf( __( ‘Also posted in %s’, ‘your-theme’ ), $cats_meow ) ?></span>
- <span class="meta-sep"> | </span>
- <?php endif ?>
标签模板
标签模板和分类模板几乎一样,只是它是按照标签来归类的。方法是复制 archive.php 文件然后重命名为 tag.php。
同样,我们也需要在 functions.php 文件里面添加一个 tag_ur_it() 函数 —— 也是来自 Sandbox 主题。功能就像 cats_meow() 那样,不过它是删除多余的标签,代码如下:
- // For tag lists on tag archives: Returns other tags except the current one (redundant)
- function tag_ur_it($glue) {
- $current_tag = single_tag_title( ”, ”, false );
- $separator = "\n";
- $tags = explode( $separator, get_the_tag_list( "", "$separator", "" ) );
- foreach ( $tags as $i => $str ) {
- if ( strstr( $str, ">$current_tag<" ) ) {
- unset($tags[$i]);
- break;
- }
- }
- if ( empty($tags) )
- return false;
- return trim(join( $glue, $tags ));
- } // end tag_ur_it
现在,打开 tag.php 文件,并用以下代码替代页面标题代码:
- <h1 class="page-title"><?php _e( ‘Tag Archives:’, ‘your-theme’ ) ?> <span><?php single_tag_title() ?></span></h1>
然后在 .entry-utility 标签里找到以下代码:
- <?php the_tags( ‘<span class="tag-links"><span class="entry-utility-prep entry-utility-prep-tag-links">’ . __(‘Tagged ‘, ‘your-theme’ ) . ‘</span>’, ", ", "</span>\n\t\t\t\t\t\t<span class=\"meta-sep\">|</span>\n" ) ?>
把上面的代码替换成下面这个:
- <?php if ( $tag_ur_it = tag_ur_it(‘, ‘) ) : // Returns tags other than the one queried ?>
- <span class="tag-links"><?php printf( __( ‘Also tagged %s’, ‘your-theme’ ), $tag_ur_it ) ?></span>
- <?php endif; ?>
这样一来,标签模板就做好了。
如何创建 WordPress 主题
这篇文章是如何创建 WordPress 主题教程系列的其中一部分,该系列将会教您如何从零开始创建强大的 WordPress 主题。建议您从头开始阅读这个系列并自己动手编写一些漂亮的代码。
相关博文:
