在之前的課程中,您已經了解并學習了主題框架的工作方式和開發途徑。
現在是時候深入探討一些代碼了!
在本教程中,您會采用一個基本的主題,然后編輯模板文件,為主題框架添加相關掛鉤和函數做好準備。本教程旨在整理主題,減少代碼重復,這意味著您要為循環建立包含文件(include files)。
您將不必在子主題中創建重復循環,當您創建新的模板文件時,或者您需要編輯循環時,您只需做一次就行了。
您需要做的是
跟隨本教程,您需要
安裝一個WordPress開發環境
GitHub庫相關系列中的起始文件或者起始主題文件
一個代碼編輯器
為循環建立包含文件
我會為我的框架建立三個循環:
一個用于存檔(包括主博客頁面)
一個用于單篇文章
一個用于頁面
這是因為我想讓其中的每一個循環與其他的顯示起來都略有不同。
盡管將會有三個循環,但相比較于每一個模板文件中都包含一個循環而言,這會更加高效。
主循環
主循環會用于存檔和主博客頁面。在您的主題文件夾中,創建一個名為loop.php的文件。
從archive.php中將下列代碼復制到loop.php文件中:
<?php
/* Queue the first post, that way we know if this is a date archive so we can display the correct title.
* We reset this later so we can run the loop properly with a call to rewind_posts().
*/
if ( have_posts() )
the_post();
?>
<h2 class="page-title">
<?php if ( is_day() ) { ?>
Archive for <?php echo get_the_date();
}
elseif ( is_month() ) { ?>
Archive for <?php echo get_the_date('F Y');
}
elseif ( is_year() ) { ?>
Archive for <?php echo get_the_date('Y');
}
else {
echo get_queried_object()->name;
} ?>
</h2>
<?php rewind_posts(); ?>
<?php // start the loop ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2 class="entry-title">
<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'compass' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
<?php the_title(); ?>
</a>
</h2>
<section class="left image quarter">
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'medium', array(
'class' => 'left',
'alt' => trim(strip_tags( $wp_postmeta->_wp_attachment_image_alt ))
) ); ?>
</a>
<?php } ?>
</section><!-- .image -->
<section class="entry-meta">
<p>Posted on <?php the_date(); ?> by <?php the_author(); ?></p>
</section><!-- .entry-meta -->
<section class="entry-content">
<?php the_content(); ?>
</section><!-- .entry-content -->
<section class="entry-meta">
<?php if ( count( get_the_category() ) ) : ?>
<span class="cat-links">
Categories: <?php echo get_the_category_list( ', ' ); ?>
</span>
<?php endif; ?>
</section><!-- .entry-meta -->
</article><!-- #01-->
<?php endwhile; ?>
<?php // ends the loop ?>
您并不需要去顯示主博客頁面上的標題,所以在第一個循環上添加一個條件標簽,以檢查我們是不是該網頁上:
if ( ! is_front_page() ) {
}
第一個循環當前會如下所示:
if ( ! is_front_page() ) {
if ( have_posts() )
the_post();
?>
<h2 class="page-title">
<?php if ( is_day() ) { ?>
Archive for <?php echo get_the_date();
}
elseif ( is_month() ) { ?>
Archive for <?php echo get_the_date('F Y');
}
elseif ( is_year() ) { ?>
Archive for <?php echo get_the_date('Y');
}
else {
echo get_queried_object()->name;
} ?>
</h2>
<?php rewind_posts();
} ?>
現在,您需要在相關網站模版文件中包含這個循環。在archive.php和index.php文件中,將現有的循環替換為get_template_part()標簽,其中包含了您的循環文件:
<?php get_template_part( 'loop' ); ?>
現在您有了一個用于存檔的工作循環。
頁面循環
接下來,您將為頁面創建一個循環文件。創建一個名為loop-page.php的文件。
從現有的page.php文件中將下列循環代碼復制到loop-page.php文件:
<?php
// Run the page loop to output the page content.
if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( ! is_front_page() ) { ?>
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php } ?>
<section class="entry-content">
<?php the_content(); ?>
</section><!-- .entry-content -->
</article><!-- #post-## -->
<?php endwhile; ?>
現在在主題的所有頁面模板中(page.php 和 page-full-width.php),使用下面的代碼替換循環:
<?php get_template_part( 'loop' , 'page' ); ?>
文章頁循環
最后,您將為單篇文章頁面創建一個循環文件,用于普通的文章和您將來創建的任何自定義的文章類型。這和主循環是相似的,只是它不包括該文章的鏈接,也沒有初始循環,用來檢查我們的存檔情況。
建立一個名為loop-single.php和另一個名為single.php的文件。
將index.php文件中的內容復制到single.php文件,并在文件的初始位置編輯說明和循環,如下所示:
<?php get_template_part( 'loop', 'single' ); ?>
現在,在single-loop.php文件中,復制代碼到loop.php文件,不包括查詢檔案的第一個循環。在循環內編輯初始標題標簽并取消鏈接,代碼如下:
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2 class="entry-title">
<?php the_title(); ?>
</h2>
<section class="left image quarter">
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'medium', array(
'class' => 'left',
'alt' => trim(strip_tags( $wp_postmeta->_wp_attachment_image_alt ))
) ); ?>
</a>
<?php } ?>
</section><!-- .image -->
<section class="entry-meta">
<p>Posted on <?php the_date(); ?> by <?php the_author(); ?></p>
</section><!-- .entry-meta -->
<section class="entry-content">
<?php the_content(); ?>
</section><!-- .entry-content -->
<section class="entry-meta">
<?php if ( count( get_the_category() ) ) : ?>
<span class="cat-links">
Categories: <?php echo get_the_category_list( ', ' ); ?>
</span>
<?php endif; ?>
</section><!-- .entry-meta -->
</article><!-- #01-->
<?php endwhile; ?>
保存這兩個文件?,F在,所有的循環文件您都準備好了。
小結
從長遠來看,使用一個主題框架之前,先整理主題并減少代碼重復將會節省下不少的工作時間。
當您開始創建子主題并和父主題一起使用時,您會發現自己在建立自定義循環的同時,也在以一種完全正確的方式完成一個給定項目的內容。有了三個獨立的循環,您就會避免在子主題中建立重復的模板文件,因為您只需要建立重復循環文件就行了。
|
新聞熱點
疑難解答