在之前的课程中,您已经了解并学习了主题框架的工作方式和开发途径。

现在是时候深入探讨一些代码了!

在本教程中,您会采用一个基本的主题,然后编辑模板文件,为主题框架添加相关挂钩和函数做好准备。本教程旨在整理主题,减少代码重复,这意味着您要为循环建立包含文件 (include files) 。

您将不必在子主题中创建重复循环,当您创建新的模板文件时,或者您需要编辑循环时,您只需做一次就行了。

您需要做的是

跟随本教程,您需要

  • 安装一个 WordPress 开发环境
  • GitHub 库相关系列中的起始文件或者起始主题文件
  • 一个代码编辑器

为循环建立包含文件

我会为我的框架建立三个循环:

  • 一个用于存档 (包括主博客页面)
  • 一个用于单篇文章
  • 一个用于页面

这是因为我想让其中的每一个循环与其他的显示起来都略有不同。

尽管将会有三个循环,但相比较于每一个模板文件中都包含一个循环而言,这会更加高效。

主循环

主循环会用于存档和主博客页面。在您的主题文件夹中,创建一个名为 loop.php 的文件。

从 archive.php 中将下列代码复制到 loop.php 文件中:

  1. <?php
  2. /* Queue the first post, that way we know if this is a date archive so we can display the correct title.
  3.  * We reset this later so we can run the loop properly with a call to rewind_posts().
  4.  */
  5. if ( have_posts() )
  6.     the_post();
  7. ?>
  8.  
  9.         <h2 >
  10.             <?php if ( is_day() ) { ?>
  11.                 Archive for <?php echo get_the_date();
  12.             }
  13.             elseif ( is_month() ) { ?>
  14.                 Archive for <?php echo get_the_date('F Y');
  15.             }
  16.             elseif ( is_year() ) { ?>
  17.                 Archive for <?php echo get_the_date('Y');
  18.             }
  19.             else {
  20.                 echo get_queried_object()->name; 
  21.             } ?>
  22.         </h2>
  23.  
  24. <?php rewind_posts(); ?>
  25.  
  26.  
  27. <?php // start the loop ?> 
  28. <?php while ( have_posts() ) : the_post(); ?>
  29.  
  30. <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
  31.  
  32.     <h2 >
  33.         <a href="http://span<?php/span%20the_permalinkspan(/spanspan)/spanspan;/span%20span?>/span" title="<?php printf( esc_attr__( 'Permalink to %s', 'compass' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
  34.             <?php the_title(); ?>
  35.         </a>
  36.     </h2>
  37.  
  38.     <section >
  39.  
  40.         <?php if ( has_post_thumbnail() ) { ?>
  41.             <a href="http://span<?php/span%20the_permalinkspan(/spanspan)/spanspan;/span%20span?>/span">
  42.                 <?php the_post_thumbnail( 'medium', array(
  43.                     'class' => 'left',
  44.                     'alt'   => trim(strip_tags( $wp_postmeta->_wp_attachment_image_alt ))
  45.                 ) ); ?>
  46.             </a>
  47.         <?php } ?>
  48.     </section><!-- .image -->
  49.  
  50.     <section >
  51.         <p>Posted on <?php the_date(); ?> by <?php the_author(); ?></p>
  52.     </section><!-- .entry-meta -->
  53.  
  54.     <section >
  55.         <?php the_content(); ?>
  56.     </section><!-- .entry-content -->
  57.  
  58.     <section >
  59.         <?php if ( count( get_the_category() ) ) : ?>
  60.             <span >
  61.                 Categories: <?php echo get_the_category_list( ', ' ); ?>
  62.             </span>
  63.         <?php endif; ?>   
  64.     </section><!-- .entry-meta -->
  65.  
  66. </article><!-- #01-->
  67.  
  68. <?php endwhile; ?>
  69. <?php // ends the loop ?>

您并不需要去显示主博客页面上的标题,所以在第一个循环上添加一个条件标签,以检查我们是不是该网页上:

  1. if ( ! is_front_page() ) {
  2. }

第一个循环当前会如下所示:

  1. if ( ! is_front_page() ) {
  2.  
  3.     if ( have_posts() )
  4.         the_post();
  5.     ?>
  6.  
  7.             <h2 >
  8.                 <?php if ( is_day() ) { ?>
  9.                     Archive for <?php echo get_the_date();
  10.                 }
  11.                 elseif ( is_month() ) { ?>
  12.                     Archive for <?php echo get_the_date('F Y');
  13.                 }
  14.                 elseif ( is_year() ) { ?>
  15.                     Archive for <?php echo get_the_date('Y');
  16.                 }
  17.                 else {
  18.                     echo get_queried_object()->name; 
  19.                 } ?>
  20.             </h2>
  21.  
  22.     <?php rewind_posts();
  23.  
  24. } ?>

现在,您需要在相关网站模版文件中包含这个循环。在 archive.php 和 index.php 文件中,将现有的循环替换为 get_template_part() 标签,其中包含了您的循环文件:

  1. <?php get_template_part( 'loop' ); ?>

现在您有了一个用于存档的工作循环。

页面循环

接下来,您将为页面创建一个循环文件。创建一个名为 loop-page.php 的文件。

从现有的 page.php 文件中将下列循环代码复制到 loop-page.php 文件:

  1. <?php
  2.     // Run the page loop to output the page content.
  3.  
  4.      if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
  5.  
  6.         <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
  7.  
  8.             <?php if ( ! is_front_page() ) { ?>
  9.                 <h2 ><?php the_title(); ?></h2>
  10.             <?php } ?>
  11.  
  12.             <section >
  13.                 <?php the_content(); ?>
  14.             </section><!-- .entry-content -->
  15.         </article><!-- #post-## -->
  16.  
  17.     <?php endwhile; ?>

现在在主题的所有页面模板中 (page.phppage-full-width.php),使用下面的代码替换循环:

  1. <?php get_template_part( 'loop' , 'page' ); ?>

文章页循环

最后,您将为单篇文章页面创建一个循环文件,用于普通的文章和您将来创建的任何自定义的文章类型。这和主循环是相似的,只是它不包括该文章的链接,也没有初始循环,用来检查我们的存档情况。

建立一个名为 loop-single.php 和另一个名为 single.php 的文件。

将 index.php 文件中的内容复制到 single.php 文件,并在文件的初始位置编辑说明和循环,如下所示:

  1. <?php get_template_part( 'loop', 'single' ); ?>

现在,在 single-loop.php 文件中,复制代码到 loop.php 文件,不包括查询档案的第一个循环。在循环内编辑初始标题标签并取消链接,代码如下:

  1. <?php while ( have_posts() ) : the_post(); ?>
  2.  
  3. <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
  4.  
  5.     <h2 >
  6.         <?php the_title(); ?>
  7.     </h2>
  8.  
  9.     <section >
  10.  
  11.         <?php if ( has_post_thumbnail() ) { ?>
  12.             <a href="http://span<?php/span%20the_permalinkspan(/spanspan)/spanspan;/span%20span?>/span">
  13.                 <?php the_post_thumbnail( 'medium', array(
  14.                     'class' => 'left',
  15.                     'alt'   => trim(strip_tags( $wp_postmeta->_wp_attachment_image_alt ))
  16.                 ) ); ?>
  17.             </a>
  18.         <?php } ?>
  19.     </section><!-- .image -->
  20.  
  21.     <section >
  22.         <p>Posted on <?php the_date(); ?> by <?php the_author(); ?></p>
  23.     </section><!-- .entry-meta -->
  24.  
  25.     <section >
  26.         <?php the_content(); ?>
  27.     </section><!-- .entry-content -->
  28.  
  29.     <section >
  30.         <?php if ( count( get_the_category() ) ) : ?>
  31.             <span >
  32.                 Categories: <?php echo get_the_category_list( ', ' ); ?>
  33.             </span>
  34.         <?php endif; ?>   
  35.     </section><!-- .entry-meta -->
  36.  
  37. </article><!-- #01-->
  38.  
  39. <?php endwhile; ?>

保存这两个文件。现在,所有的循环文件您都准备好了。

小结

从长远来看,使用一个主题框架之前,先整理主题并减少代码重复将会节省下不少的工作时间。

当您开始创建子主题并和父主题一起使用时,您会发现自己在建立自定义循环的同时,也在以一种完全正确的方式完成一个给定项目的内容。有了三个独立的循环,您就会避免在子主题中建立重复的模板文件,因为您只需要建立重复循环文件就行了。