国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 網(wǎng)站 > 建站經(jīng)驗(yàn) > 正文

為你的WordPress網(wǎng)站模板框架建立起始文件

2024-04-25 20:39:58
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

在之前的課程中,您已經(jīng)了解并學(xué)習(xí)了主題框架的工作方式和開(kāi)發(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í),您只需做一次就行了。

您需要做的是

跟隨本教程,您需要

安裝一個(gè)WordPress開(kāi)發(fā)環(huán)境

GitHub庫(kù)相關(guān)系列中的起始文件或者起始主題文件

一個(gè)代碼編輯器

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

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

一個(gè)用于存檔(包括主博客頁(yè)面)

一個(gè)用于單篇文章

一個(gè)用于頁(yè)面

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

盡管將會(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)網(wǎng)站模版文件中包含這個(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)

接下來(lái),您將為頁(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.php 和 page-full-width.php),使用下面的代碼替換循環(huán):

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

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

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

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

將index.php文件中的內(nèi)容復(fù)制到single.php文件,并在文件的初始位置編輯說(shuō)明和循環(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)來(lái)看,使用一個(gè)主題框架之前,先整理主題并減少代碼重復(fù)將會(huì)節(jié)省下不少的工作時(shí)間。

當(dāng)您開(kāi)始創(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)文件就行了。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 钦州市| 郧西县| 武鸣县| 北票市| 永清县| 射阳县| 潜江市| 应城市| 江阴市| 金塔县| 旺苍县| 兴义市| 鹿邑县| 岳池县| 顺平县| 色达县| 平舆县| 库伦旗| 江华| 辰溪县| 大英县| 惠安县| 额尔古纳市| 普安县| 依兰县| 普兰店市| 长沙县| 阿坝县| 紫云| 嵊州市| 朝阳市| 通化市| 兰州市| 宣城市| 林西县| 大埔县| 库车县| 安图县| 澎湖县| 海阳市| 开封市|