當(dāng)前位置:首頁(yè)>WordPress建站>WordPress開發(fā)>為你的WordPress主題框架建立起始文件

為你的WordPress主題框架建立起始文件

在之前的課程中,您已經(jīng)了解并學(xué)習(xí)了主題框架的工作方式和開發(fā)途徑。

現(xiàn)在是時(shí)候深入探討一些代碼了!

在本教程中,您會(huì)采用一個(gè)基本的主題,然后編輯模板文件,為主題框架添加相關(guān)掛鉤和函數(shù)做好準(zhǔn)備。本教程旨在整理主題,減少代碼重復(fù),這意味著您要為循環(huán)建立包含文件(include files)。

您將不必在子主題中創(chuàng)建重復(fù)循環(huán),當(dāng)您創(chuàng)建新的模板文件時(shí),或者您需要編輯循環(huán)時(shí),您只需做一次就行了。

注:起始文件在我的系列課程《從靜態(tài)HTML中創(chuàng)建一個(gè)WordPress主題》而創(chuàng)建的基礎(chǔ)之上,稍作了些改動(dòng)。您可以從GitHub庫(kù)相關(guān)系列中去下載它們。

您需要做的是

跟隨本教程,您需要

  • 安裝一個(gè)WordPress開發(fā)環(huán)境
  • GitHub庫(kù)相關(guān)系列中的起始文件或者起始主題文件
  • 一個(gè)代碼編輯器

為循環(huán)建立包含文件

我會(huì)為我的框架建立三個(gè)循環(huán):

  • 一個(gè)用于存檔(包括主博客頁(yè)面)
  • 一個(gè)用于單篇文章
  • 一個(gè)用于頁(yè)面

這是因?yàn)槲蚁胱屍渲械拿恳粋€(gè)循環(huán)與其他的顯示起來都略有不同。

盡管將會(huì)有三個(gè)循環(huán),但相比較于每一個(gè)模板文件中都包含一個(gè)循環(huán)而言,這會(huì)更加高效。

主循環(huán)

主循環(huán)會(huì)用于存檔和主博客頁(yè)面。在您的主題文件夾中,創(chuàng)建一個(gè)名為loop.php的文件。

從archive.php中將下列代碼復(fù)制到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 ?>

您并不需要去顯示主博客頁(yè)面上的標(biāo)題,所以在第一個(gè)循環(huán)上添加一個(gè)條件標(biāo)簽,以檢查我們是不是該網(wǎng)頁(yè)上:

if ( ! is_front_page() ) {
}

第一個(gè)循環(huán)當(dāng)前會(huì)如下所示:

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();
 
} ?>

現(xiàn)在,您需要在相關(guān)模板文件中包含這個(gè)循環(huán)。在archive.php和index.php文件中,將現(xiàn)有的循環(huán)替換為get_template_part()標(biāo)簽,其中包含了您的循環(huán)文件:

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

現(xiàn)在您有了一個(gè)用于存檔的工作循環(huán)。

頁(yè)面循環(huán)

接下來,您將為頁(yè)面創(chuàng)建一個(gè)循環(huán)文件。創(chuàng)建一個(gè)名為loop-page.php的文件。

從現(xiàn)有的page.php文件中將下列循環(huán)代碼復(fù)制到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; ?>

現(xiàn)在在主題的所有頁(yè)面模板中(page.phppage-full-width.php),使用下面的代碼替換循環(huán):

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

文章頁(yè)循環(huán)

最后,您將為單篇文章頁(yè)面創(chuàng)建一個(gè)循環(huán)文件,用于普通的文章和您將來創(chuàng)建的任何自定義的文章類型。這和主循環(huán)是相似的,只是它不包括該文章的鏈接,也沒有初始循環(huán),用來檢查我們的存檔情況。

建立一個(gè)名為loop-single.php和另一個(gè)名為single.php的文件。

將index.php文件中的內(nèi)容復(fù)制到single.php文件,并在文件的初始位置編輯說明和循環(huán),如下所示:

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

現(xiàn)在,在single-loop.php文件中,復(fù)制代碼到loop.php文件,不包括查詢檔案的第一個(gè)循環(huán)。在循環(huán)內(nèi)編輯初始標(biāo)題標(biāo)簽并取消鏈接,代碼如下:

<?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; ?>

保存這兩個(gè)文件。現(xiàn)在,所有的循環(huán)文件您都準(zhǔn)備好了。

小結(jié)

從長(zhǎng)遠(yuǎn)來看,使用一個(gè)主題框架之前,先整理主題并減少代碼重復(fù)將會(huì)節(jié)省下不少的工作時(shí)間。

當(dāng)您開始創(chuàng)建子主題并和父主題一起使用時(shí),您會(huì)發(fā)現(xiàn)自己在建立自定義循環(huán)的同時(shí),也在以一種完全正確的方式完成一個(gè)給定項(xiàng)目的內(nèi)容。有了三個(gè)獨(dú)立的循環(huán),您就會(huì)避免在子主題中建立重復(fù)的模板文件,因?yàn)槟恍枰⒅貜?fù)循環(huán)文件就行了。

聲明:本站所有文章,如無特殊說明或標(biāo)注,均為本站原創(chuàng)發(fā)布。任何個(gè)人或組織,在未征得本站同意時(shí),禁止復(fù)制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺(tái)。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。

給TA打賞
共{{data.count}}人
人已打賞
歡迎關(guān)注WordPress大學(xué)公眾號(hào) WPDAXUE
WordPress開發(fā)

決定如何開發(fā)你的WordPress主題框架

2015-2-8 11:21:22

WordPress開發(fā)

為你的WordPress主題框架添加動(dòng)作掛鉤

2015-2-14 21:31:51

3 條回復(fù) A文章作者 M管理員
  1. zeroYang

    ? ? ? 看到這就看不懂了..可能是我理解能力太差了,有視頻什么的么

  2. 李小遙

    “Now in single-loop.php, copy the code in loop.php, not including the first loop looking for archives”這樣翻譯似乎更準(zhǔn)確一點(diǎn):復(fù)制loop.php中的代碼到single.php文件。注意不要包含用于存檔的第一個(gè)循環(huán)。

  3. 過來學(xué)習(xí)了,授人以魚不如授人以漁,感謝 :mrgreen:

?
個(gè)人中心
購(gòu)物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

中超| 庆云县| 峨眉山市| 保山市| 富源县| 吴忠市| 得荣县| 罗平县| 江门市| 宁河县| 横山县| 米泉市| 淳化县| 蒙自县| 奉新县| 隆林| 南皮县| 漾濞| 临桂县| 教育| 小金县| 朝阳市| 中超| 尤溪县| 嘉兴市| 芮城县| 双辽市| 东乌珠穆沁旗| 小金县| 扎赉特旗| 竹北市| 綦江县| 浑源县| 保定市| 开江县| 平湖市| 天祝| 邵武市| 阿图什市| 五指山市| 绥阳县|