原文:The WordPress Theme Search Template and Page Template
原文作者:Ian Stewart
译文作者:Young
对任一完整的 WordPress 主题来说,搜索模板和静态页面模板都是很重要的,并且代码都很容易。这两个模板也是模板中的模板,都有以下相同的代码:
-
<?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(); ?>
当然,每个模板也都互不相同。
搜索模板
在 serach.php 里面,我们需要重新用到循环。这次使用 IF 语句 —— 以防循环里没有任何文章。
工作原理是这样的:如果有文章,或者说有符合搜索条件的文章,那么就循环检测,就像 index.php 里面的循环语句一样。但是如果没有文章,或者没有符合搜索条件的文章,那就为搜索者提供另外一个搜索机会。
代码如下:
-
-
-
<?php get_header(); ?>
-
-
<div id="container">
-
<div id="content">
-
-
-
<?php if ( have_posts() ) : ?>
-
-
<?php while ( have_posts() ) : the_post() ?>
-
-
<!– this is our loop –>
-
<?php endwhile; ?>
-
-
<?php else : ?>
-
-
-
<!– here’s where we’ll put a search form if there’re no posts –>
-
-
<?php endif; ?>
-
-
</div><!– #content –>
-
</div><!– #container –>
-
-
-
<?php get_sidebar(); ?>
-
<?php get_footer(); ?>
-
非常直接了当,是吧?
我想保留日志标题、元、内容(或摘要)以及实用链接等 index 模板里面有的内容不变,但是 WordPress 搜索日志的时候同时也会搜索静态页面,而静态页面是不需要元或者实用链接之类的,所以,在我们的循环里面,将会检测看是搜索日志还是搜索静态页面。
-
<?php if ( $post->post_type == ‘post’ ) { ?>
-
-
<?php } ?>
这个 IF 语句只有搜索静态页面的时候才会显示,现在我们知道会发生什么事了,以下就是搜索模板 #content DIV 标签里面的代码:
-
<?php if ( have_posts() ) : ?>
-
-
-
<h1 class="page-title"><?php _e( ‘Search Results for: ‘, ‘your-theme’ ); ?><span><?php the_search_query(); ?></span></h1>
-
-
-
<?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>
-
-
-
<?php if ( $post->post_type == ‘post’ ) { ?>
-
<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 –>
-
<?php } ?>
-
-
-
<div class="entry-summary">
-
<?php the_excerpt( __( ‘Continue reading <span class="meta-nav">»</span>’, ‘your-theme’ ) ); ?>
-
-
<?php wp_link_pages(‘before=<div class="page-link">’ . __( ‘Pages:’, ‘your-theme’ ) . ‘&after=</div>’) ?>
-
-
</div><!– .entry-summary –>
-
-
<?php if ( $post->post_type == ‘post’ ) { ?>
-
<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 –>
-
<?php } ?>
-
</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 } ?>
-
-
<?php else : ?>
-
-
-
<div id="post-0" class="post no-results not-found">
-
<h2 class="entry-title"><?php _e( ‘Nothing Found’, ‘your-theme’ ) ?></h2>
-
-
<div class="entry-content">
-
<p><?php _e( ‘Sorry, but nothing matched your search criteria. Please try again with some different keywords.’, ‘your-theme’ ); ?></p>
-
-
<?php get_search_form(); ?>
-
</div><!– .entry-content –>
-
-
</div>
-
-
<?php endif; ?>
【当然,您还可以加上页眉和页脚以及侧边栏等函数。——译者注】
静态页面模板
您知道静态页面模板是干什么用的。WordPress 认为它是一种没有时间顺序的日志。我们认为它是一个静态页面,不过日志的所有装饰它都几乎没有——除了评论,有些静态页面是有评论的。我不喜欢,不过您可能喜欢。如果您想在静态页面添加评论,可以在静态页面上添加带有 Name 和 Value 的 “comments”自定义区域,这个我听起来不错。
下面就是静态页面代码:
-
<?php get_header(); ?>
-
-
-
<div id="container">
-
<div id="content">
-
-
<?php the_post(); ?>
-
-
-
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
-
<h1 class="entry-title"><?php the_title(); ?></h1>
-
-
<div class="entry-content">
-
<?php the_content(); ?>
-
<?php wp_link_pages(‘before=<div class="page-link">’ . __( ‘Pages:’, ‘your-theme’ ) . ‘&after=</div>’) ?>
-
-
<?php edit_post_link( __( ‘Edit’, ‘your-theme’ ), ‘<span class="edit-link">’, ‘</span>’ ) ?>
-
-
</div><!– .entry-content –>
-
</div><!– #post-<?php the_ID(); ?> –>
-
-
-
<?php if ( get_post_custom_values(‘comments’) ) comments_template() // Add a custom field with Name and Value of "comments" to enable comments on this page ?>
-
-
-
</div><!– #content –>
-
</div><!– #container –>
-
-
<?php get_sidebar(); ?>
-
-
<?php get_footer(); ?>
如何创建 WordPress 主题
这篇文章是如何创建 WordPress 主题教程系列的其中一部分,该系列将会教您如何从零开始创建强大的 WordPress 主题。建议您从头开始阅读这个系列并自己动手编写一些漂亮的代码。
原创文章,转载请注明:转载自精品博客
本文链接地址: WordPress 主题之搜索模板和静态页面模板
相关博文:

3 条评论
看不懂,凑热闹
纯粹支持,新主题很简洁。
@芒果 ,谢谢。这样的文章其实主要是供自己学习用的,如果有人想学做主题,我会建议他们直接看原文并且多和原作者讨论。:-)