久久精品水蜜桃av综合天堂,久久精品丝袜高跟鞋,精品国产肉丝袜久久,国产一区二区三区色噜噜,黑人video粗暴亚裔

WordPress: The Loop in Action:修訂間差異

來自站長百科
跳轉(zhuǎn)至: 導航、? 搜索
無編輯摘要
無編輯摘要
第338行: 第338行:


</pre>
</pre>
===Different Headers/Sidebars/Footers===
WordPress offers the <tt>get_header()</tt>, <tt>get_sidebar()</tt>, and <tt>get_footer()</tt> [[WordPress:Include Tags]] for use in your [[WordPress:Templates|template files]].? These functions make it easy to define a standard header/sidebar/footer which is easily editable.? Any changes made to these files will immediately be made visible to viewers, without any work on your part.


===不同的頁眉/邊欄/頁腳===
===不同的頁眉/邊欄/頁腳===
WordPress在[[WordPress:Templates|模板文件]]中提供<tt>get_header()</tt>,<tt>get_sidebar()</tt>, 和<tt>get_footer()</tt> 幾個[[WordPress:Include Tags|包含標簽]]使用。這些函數(shù)讓定義標準的頁眉/邊欄/頁腳更加簡單。任何對這些文件的改動會立刻顯示給訪問者,你不用做任何工作。
WordPress在[[WordPress:Templates|模板文件]]中提供<tt>get_header()</tt>,<tt>get_sidebar()</tt>, 和<tt>get_footer()</tt> 幾個[[WordPress:Include Tags|包含標簽]]使用。這些函數(shù)讓定義標準的頁眉/邊欄/頁腳更加簡單。任何對這些文件的改動會立刻顯示給訪問者,你不用做任何工作。
But sometimes you might not <em>want</em> a sidebar.? If you don't want a sidebar, simply exclude the call to the <tt>get_sidebar()</tt> function from your template.? For example, the <tt>single.php</tt> template in the WordPress default theme does not include a sidebar.


有時你可能不<em>想</em>要邊欄。如果你不想要邊欄,只需簡單的把<tt>get_sidebar()</tt>函數(shù)的調(diào)用從模板中刪除。如,WordPress 默認主題中的模板不包括<tt>single.php</tt>。
有時你可能不<em>想</em>要邊欄。如果你不想要邊欄,只需簡單的把<tt>get_sidebar()</tt>函數(shù)的調(diào)用從模板中刪除。如,WordPress 默認主題中的模板不包括<tt>single.php</tt>。


To create your own <strong>different</strong> sidebar, you have two choices.
建立你自己的<strong>不同的</strong> 邊欄,你有兩種選擇。
# Include the sidebar contents directly into the template file on which you're working.? If you want category-3 to have a different sidebar, edit <tt>category-3.php</tt> and include the necessary HTML and PHP to generate your distinctive sidebar.
# Use the PHP <tt>[http://www.php.net/include include]</tt> function, to include another file.? The WordPress <tt>get_sidebar()</tt> function <em>only</em> loads <tt>sidebar.php</tt>.? If you make a file named <tt>sideleft.php</tt>, you would include it like this:
<pre>
&lt;?php include(TEMPLATEPATH . '/sideleft.php'); ?&gt;
</pre>
?
眼建立你自己的<strong>不同的</strong> 邊欄,你有兩種選擇。
# 把邊欄內(nèi)容直接包含到你正在操作的模板文件中。如果你想要category-3 擁有不同的邊欄, 編輯<tt>category-3.php</tt>,把必須的HTML和PHP代碼包含進去來生成這個唯一的邊欄。
# 把邊欄內(nèi)容直接包含到你正在操作的模板文件中。如果你想要category-3 擁有不同的邊欄, 編輯<tt>category-3.php</tt>,把必須的HTML和PHP代碼包含進去來生成這個唯一的邊欄。
# 使用PHP的<tt>[http://www.php.net/include 包含]</tt> 函數(shù), 來包含另外一個文件。 WordPress <tt>get_sidebar()</tt> 函數(shù) <em>只</em> 加載<tt>sidebar.php</tt>. 如果你有個文件名為<tt>sideleft.php</tt>, 你可以象這樣把它涵蓋進去::
# 使用PHP的<tt>[http://www.php.net/include 包含]</tt> 函數(shù), 來包含另外一個文件。 WordPress <tt>get_sidebar()</tt> 函數(shù) <em>只</em> 加載<tt>sidebar.php</tt>. 如果你有個文件名為<tt>sideleft.php</tt>, 你可以象這樣把它涵蓋進去::
第362行: 第350行:
&lt;?php include(TEMPLATEPATH . '/sideleft.php'); ?&gt;
&lt;?php include(TEMPLATEPATH . '/sideleft.php'); ?&gt;
</pre>
</pre>
Using the WordPress default [[WordPress:Template Hierarchy]], if you want to use the same elements on multiple or different templates, it's probably best to put them in separate template files and use the PHP <tt>include()</tt> function.? If the element you're adding is specifically for one template file, it's probably best to include it directly in that template file.


使用WordPress默認的[[WordPress:Template Hierarchy|模板層次]],如果你想在多個或者不同的模板中使用相同的元素,你最好把它們放到分開的模板文件中,并使用PHP<tt>include()</tt>函數(shù)。如果這個你添加的元素是特別為某一模板使用的,最好是把它直接包含到那個模板中去。
使用WordPress默認的[[WordPress:Template Hierarchy|模板層次]],如果你想在多個或者不同的模板中使用相同的元素,你最好把它們放到分開的模板文件中,并使用PHP<tt>include()</tt>函數(shù)。如果這個你添加的元素是特別為某一模板使用的,最好是把它直接包含到那個模板中去。
==Summary==
We've just scratched the surface of what can be done with the Loop.? As a reminder, the following are resources that will help you customize your own [[WordPress:The Loop|WordPress Loop]].
* [[WordPress:Templates|Template Files]]
* [[WordPress:Template Tags]]
* [[WordPress:Template Hierarchy]]
* [[WordPress:Conditional Tags]]


==概要==
==概要==
We've just scratched the surface of what can be done with the Loop.? As a reminder, the following are resources that will help you customize your own [[WordPress:The Loop|WordPress Loop]].
* [[WordPress:Templates|Template Files]]
* [[WordPress:Template Tags]]
* [[WordPress:Template Hierarchy]]
* [[WordPress:Conditional Tags]]
我們剛才簡單說了下我們使用Loop能做些什么。作為提醒,下邊的內(nèi)容可能會幫助你自定義你自己的[[WordPress:The Loop|WordPress Loop]]。
我們剛才簡單說了下我們使用Loop能做些什么。作為提醒,下邊的內(nèi)容可能會幫助你自定義你自己的[[WordPress:The Loop|WordPress Loop]]。


第390行: 第361行:
* [[WordPress:Conditional Tags|條件式標簽]]
* [[WordPress:Conditional Tags|條件式標簽]]


==Resources==
* [http://www.obeattie.com/2006/05/02/wordpress-the-loop/ oBeattie : The Loop]
* [http://www.themelab.com/2008/04/04/the-ultimate-guide-to-the-wordpress-loop/ The Ultimate Guide to the WordPress Loop]


==資源==
==資源==
* [http://www.obeattie.com/2006/05/02/wordpress-the-loop/ oBeattie : The Loop]
* [http://www.obeattie.com/2006/05/02/wordpress-the-loop/ oBeattie : The Loop]
* [http://www.themelab.com/2008/04/04/the-ultimate-guide-to-the-wordpress-loop/ WordPress Loop終極指南]
* [http://www.themelab.com/2008/04/04/the-ultimate-guide-to-the-wordpress-loop/ WordPress Loop終極指南]

2008年4月22日 (二) 13:37的版本

介紹

"The Loop"是指WordPress主程序中的一個術語。你可以在你的模板文件中使用Loop來想訪問者顯示你的文章。你可以讓模板不帶有Loop,但這樣你就只能顯示來自一篇文章的數(shù)據(jù)了。

WordPress所做的第一件事就是檢查它所需要的所有文件是否存在。接下來,它按照blog 管理員定義的,從數(shù)據(jù)庫收集默認的設置。這包括每頁顯示的文章的數(shù)量,是否允許評論,等等。一旦這些默認的內(nèi)容確定后,WordPress就會檢查使用者要求什么。這個信息用來確定從數(shù)據(jù)庫中取出哪篇文章。s

如果使用者不要求特定的文章,分類,頁面或者日期,WordPress使用以前收集的默認值確定哪個文章準備給使用者閱讀。例如,如果blog管理員在Administration > Settings > Reading設置中選擇每頁顯示5篇文章,那么WordPress就會從數(shù)據(jù)庫中取最新的5篇。如果使用者要求特定的文章,分類,頁面或者日期,那么WordPress就會根據(jù)這些信息來選擇從數(shù)據(jù)庫中取出哪篇文章

一旦所有這些完成之后,WordPress就連接數(shù)據(jù)庫,得到需要的信息,然后在一個變量中儲存這個結(jié)果。進入這個變量的就是Loop,然后在模板中顯示變量的值。 默認情況下,如果訪問者沒有選擇特定的文章,頁面,分類或者日期,WordPress會使用index.php顯示所有內(nèi)容。在這個Loop的討論的第一部分,我們只集中討論index.php,和默認的blog顯示。接下來,一旦你懂得這些如何工作的時候,我們來研究Loop在別的模板文件中的作用。

世界上最簡單的索引頁面

接下來的是具有所有功能的索引,可以顯示每個文章的內(nèi)容(僅僅是內(nèi)容),根據(jù)使用情況準備Loop,給你看這些的唯一目的就是表明這些對Loop的技能幾乎沒有必要,在你的index.php文件大多數(shù)內(nèi)容中,都是CSS, HTML,和 PHP聲明,可以讓Loop看起來漂亮些。

<?php
get_header();
if (have_posts()) :
   while (have_posts()) :
      the_post();
      the_content();
   endwhile;
endif;
get_sidebar();
get_footer(); 
?>

默認 Loop

接下來是一步一步的來看WordPress v1.5標準安裝,默認情況下,默認經(jīng)典主題中l(wèi)oop用法。

Loop開始

在默認index.php模板文件的頂部,是Loop代碼開始的地方。

<?php if (have_posts()) : ?><br />
<?php while (have_posts()) : the_post(); ?>
  1. 首先它用have_posts()功能檢查是否有新的文章.
  2. 如果有文章的話,一個PHP while的loop就開始了。只要插入語為邏輯真,while loop就會繼續(xù)執(zhí)行。這樣只要函數(shù)have_posts() 返回真值,Loop就會繼續(xù)。
  3. 函數(shù)have_posts()簡單的在文章集合中檢查下一個項目:如果有另外一個項目,返回true,如果沒有下一個項目,返回false .

生成文章

the_post()函數(shù)把文章集合中的現(xiàn)用的項目拿出來,并且讓它可以在Loop的循環(huán)中使用。如果沒有the_post(),很多主題中使用的模板標簽都無法工作了。

一旦文章日期被設置可用,模板就能啟用對來訪者展示文章數(shù)據(jù)。

標題,日期和作者

下邊的 模板標簽獲得當前文章的標題,還有發(fā)表的時間和誰發(fā)表了它。

<h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>

文章內(nèi)容

the_content()模板標簽顯示文章的內(nèi)容。 這是每個通過Loop的很重要的部分:

<div class="entry">
<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>

如果你把more這個按鈕包含到Quicktag,然后象這樣<!--more-->顯示出來, 在你的文章的正文部分,只有above這部分才會顯示給訪問者。這樣,如果你只是想讓你的首頁面顯示每篇文章的第一句或者前兩句話的話,只需要簡單的在每篇文章的第一行之后插入 <!--more-->就可以了。

當查看某個單一的文章的時候,<!-- more -->分隔符會被忽略。這樣如果讀者想閱讀全部內(nèi)容,而又在所有的文章中都加如<!-- more -->分隔符的話,會迫使讀者點擊每個單獨的文章。

詳細資料附

在每個文章內(nèi)容下邊,在index.php模板文件中,是顯示有關這個文章更多信息的地方,如分類,日期和評論信息。大家知道的文章meta數(shù)據(jù)部分,如果你是一個已經(jīng)登陸的有充分權限的使用者,你可會看見"Edit This"連接,這多虧了edit_post_link()模板標簽函數(shù)。

<p class="postmetadata">
Posted in <?php the_category(', ') ?> 
<strong>|</strong>
<?php edit_post_link('Edit','','<strong>|</strong>'); ?>  
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>

如果評論被激活,或者是該文章有評論內(nèi)容,comments_popup_link()模板標簽將會顯示一個到評論內(nèi)容的連接。如果你使用了評論彈出窗口,這個連接就會打開評論窗口,否則它就會直接跳轉(zhuǎn)到這篇文章的評論內(nèi)容。

如果訪問者在瀏覽文章索引(i.e.: 在Loop中不止一篇文章),comments_popup_link()連接將會把讀者連接到這篇文章的單獨頁面。

自動尋找Trackback

trackback_rdf模板標簽的功能是輸出機讀代碼用于自動尋找trackback

<!--
<?php trackback_rdf(); ?>
-->

注意:trackback_rdf()標簽支持在自身旁邊使用評論。它并非"關閉"的。

結(jié)束 Loop

下面是結(jié)束 Loop。在這之后,各種文章相關的模板標簽不再如如你想要的那樣工作了(如果它們在工作,它們會使用Loop的最后一篇文章).這意味著,如果你需要使用一個工作在 Loop內(nèi)的模板標簽,你需要把如下語句放進去。

<?php endwhile; ?>

在這個部分,在Loop結(jié)束后立即通過每個網(wǎng)頁顯示出向前還是向后的導航控制。

<div class="navigation">
<div class="alignleft"><?php posts_nav_link('','','&laquo; Previous Entries') ?></div>
<div class="alignright"><?php posts_nav_link('','Next Entries &raquo;','') ?></div>
</div>

如果blog設置成每頁顯示10篇文章,而且Loop收集到了25篇文章,這就會產(chǎn)生三個頁面:兩個10篇文章的頁面,還有一個五篇的頁面。導航連接允許訪問者通過文章收集跳轉(zhuǎn)到上一頁還是下一頁。

導航控制是不包含在Loop內(nèi)的,但是包含在if 條件句內(nèi),這樣它們只能顯示是否有文章。導航函數(shù)本身也會檢查是否有一些基于現(xiàn)有Loop的,它們能連接的內(nèi)容,如果有可連接的內(nèi)容的話只顯示連接。

<?php else : ?>
 <h2 class="center">Not Found</h2>
 <p class="center">
<?php _e("Sorry, but you are looking for something that isn't here."); ?></p>

else :語句決定了如果have_posts()返回false時做些什么。那就是說,else之后的部分只能在Loop沒有文章時才會被顯示。沒有文章顯示出來,舉例來說,訪問者要求某個特殊日子的內(nèi)容,但是那天沒有文章,或者是搜索但是沒有結(jié)果。

  <?php endif; ?>

這個語句結(jié)束了這樣的條件句:"如果有文章這樣,如果沒有文章那樣".一旦條件語句結(jié)束了,默認的index.php模板包括了邊欄,最后是頁腳。

其它模板中的Loop

WordPress可以使用不同的模板文件,用不同的方式顯示你的blog。在默認WordPress主題中,有用于索引瀏覽的模板文件,分類瀏覽和文檔瀏覽,就象一個瀏覽單獨文章的模板。這些都會用到Loop,但是這些都沒有太大的差距,就象模板標簽之間的不同用法似的。

至于那些沒有分開的模板文件的情況,WordPress使用默認時的index.php。如果訪問者請求閱讀一個單獨的文章時,WordPress會首先查找一個名字為single.php的文件。如果這個文件存在,它就會用來顯示這個文章。如果不存在,WordPress就會使用index.php來顯示文章。這叫做模板層次.

如果你正在制作你自己的主題,在默認主題中查看模板文件,作為參考,很有用。同樣,使用你的主題的index.php作為你的其他模板文件的模板也是很有用的。因為你創(chuàng)建了更多的模板文件,這樣做可能會帶給你一個已知的工作頁面,從這里開始作出更改。

不同的歸檔格式

archive是歷史文章的集合。在默認時,文章在主索引中顯示的是最新的按時間順序的記錄。當訪問者點擊某個文檔連接時,或者他們手動請求某個特定時間時,(使用http://www.example.com/blog/index.php?m=200504或者 http://www.example.com/blog/2005/04 來選擇所有2005年4月后的文章),WordPress將顯示archive內(nèi)容。默認情況下,歸檔將使用index.php,然后和你的首頁一樣,只顯示出2005年四月后的文章。

當WordPress為訪問者準備歸檔界面時,它會在你現(xiàn)用的主題目錄中明確的尋找一個叫做archive.php的文件,如果你想在首頁上使歸檔的意思明確表達,那么把index.php復制到archive.php,并且按需求編輯archive.php文件。

例如,如果你只想在歸檔列表上顯示文章標題,不包含文章內(nèi)容,你可以使用如下代碼:

<?php get_header(); ?>
 <div id="content" class="narrowcolumn">

  <?php if (have_posts()) : ?>
   <?php while (have_posts()) : the_post(); ?>
     <div class="post">
     <h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
     <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
      </div>
    <?php endwhile; ?>
<div class="navigation">
<div class="alignleft">
<?php posts_nav_link('','','&laquo; Previous Entries') ?>
</div>
<div class="alignright">
<?php posts_nav_link('','Next Entries &raquo;','') ?>
</div>
  </div>
<?php else : ?>
  <h2 class="center">Not Found</h2>
 <p class="center"><?php _e("Sorry, but you are looking for something that isn't here."); ?></p>
  <?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

不同的分類格式

和歸檔界面一樣,WordPress為分類界面尋找分開的模板文件。如果訪問者點擊了一個分類連接,他們會看到分類外觀,WordPress會只從分類中準備帶有文章的Loop,限制每個blog默認設置下的文章的數(shù)目。

要想讓你的分類界面和索引界面不同的話,復制一個index.php文件并重命名為category.php,對于分類界面,向一個分配過的文章列出分類也許不是必須的,所以我們忽略這一步。取而代之的,我們在頁面頂部聲明分類:

<?php get_header(); ?>
 <div id="content" class="narrowcolumn">
 <p>
 <strong>
  <?php single_cat_title('Currently browsing '); ?>
  </strong><br />
 <?php echo category_description(); ?>
 </p>
 <?php if (have_posts()) : ?>
   <?php while (have_posts()) : the_post(); ?>
     <div class="post">
      <h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a></h2>
   <small>
     <?php the_time('F jS, Y') ?> 
        <!-- by <?php the_author() ?> -->
   </small>
 </div>
<?php endwhile; ?>
 <div class="navigation">
   <div class="alignleft">
    <?php posts_nav_link('','','&laquo; Previous Entries') ?>
   </div>
   <div class="alignright">
    <?php posts_nav_link('','Next Entries &raquo;','') ?>
   </div>
 </div>
<?php else : ?>
  <h2 class="center">Not Found</h2>
<p class="center"><?php _e("Sorry, but you are looking for something that isn't here."); ?></p>
 <?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

不同分類的不同格式

如在模板層次中敘述的,為每個分類創(chuàng)建分開的模板文件是可行的。只需要建立名字為category-X.php的文件,這里X是用數(shù)字表示的分類。仔細考慮你是否需要給某個分類建立全新的完整模板。

讓我們來看兩個分類,"Plants" 和 "Flowers",分類ID分別為3和4。在輸出的地方的每個文章標題旁邊,你都想要有個植物或者花的圖片,取決于哪個分類被顯示,你可以這樣:

  • 使用兩個分開的文件, category-3.phpcategory-4.php, 每個文件給每個文章標題使用不同的img標簽。
  • 在你的默認category.php文件中使用條件判斷,來查看是否當前分類是"Plants"或者 "Flowers" (或者都不是), 然后顯示合適的圖片:
<?php if (is_category('3') ):
 // we're in the Plants category, so show a plant ?>
 <img src='/images/plant.png' alt='a plant' />
<?php } elseif (is_category('4') ):
 // we're in the Flowers category, so show a flower ?>
 <img src='/images/flower.png' alt='a pretty flower' />
<?php endif; // end the if, no images for other other categories ?>

如果你添加了另外一個分類,"Cars",你想讓它用一個引人注目的方式顯示出來,那么一個分開的category-X.php則是你最合適的選擇。

不同分類的不同CSS

很多使用者想給特定的分類建立單獨的CSS文件。這很容易實現(xiàn)。記住樣式表是在HTML文件中的<head>部分定義并且加載的。WordPress使用header.php文件,在默認header.php文件中,找到下列語句:

<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />

And change it to something like this:

<?php if ( is_category('5') ) { // Load special CSS for "Cars" category ?>
  <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/category-5.css" type="text/css" media="screen" />;
<?php } else { ?>
   <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
<?php } ?>

注意: 汽車分類模板使用category-5.css文件取代默認版面。在這個例子中,CSS文件在應用的分類模板文件之后被命名,而不是真正的分類名。這樣你就明白 category-5.css 是和category-5.php文件聯(lián)系在一起的了.

不同的單一文章格式

當瀏覽任意單一的文章(或者 permalink)的時候,如果有的話,WordPress會使用single.php。 在這個部分,在WordPress默認的single.php文件中,提供了有關當前文章的文章meta數(shù)據(jù)信息

<p class="postmetadata alt">
<small>
This entry was posted on <?php the_time('l, F jS, Y') ?> at <?php the_time() ?> 
and is filed under <?php the_category(', ') ?>.
You can follow any responses to this entry through 
the <?php comments_rss_link('RSS 2.0'); ?> feed.
<?php
if (('open' == $post->comment_status) && ('open' == $post->ping_status)) {
// Both Comments and Pings are open
?>
  You can <a href="#respond">leave a response</a>, or 
  <a href="<?php trackback_url(display); ?>">trackback</a> 
from your own site.
<?php 
} elseif (!('open' == $post->comment_status) && ('open' == $post->ping_status)) {
// Only Pings are Open 
?>
  Responses are currently closed, but you can 
  <a href="<?php trackback_url(display); ?> ">trackback</a> 
from your own site.
<?php
} elseif (('open' == $post->comment_status) && !('open' == $post->ping_status)) { 
// Comments are open, Pings are not 
?>
  You can skip to the end and leave a response. Pinging is currently not allowed.
<?php
} elseif (!('open' == $post->comment_status) && !('open' == $post->ping_status)) { 
// Neither Comments, nor Pings are open 
?>
  Both comments and pings are currently closed.
<?php 
} 
edit_post_link('Edit this entry.','',''); ?>
</small>
</p>

這種信息—不論評論是公開還是關閉—是相當不適合放在一個索引上,歸檔或者分類界面的;那也是為什么只包含在single.php模板文件中的原因。

其他Loop技巧

既然你已經(jīng)對WordPress Loop的基本使用有了一個全面的了解,我們開始介紹更多的Loop效果和技巧。

靜態(tài)首頁

你如何顯示一些blog中特別的僅僅存在于你的首頁上的東西呢?對,就是只在你的首頁或者主頁上的,站點上別的任何地方都看不到的。簡單!我們把這稱為靜態(tài)首頁。站點的首頁并不真的是靜態(tài)的。它只是使用了Loop讓它看起來如此罷了。

想讓這個Loop技巧實現(xiàn),使用is_home()條件式模板標簽函數(shù)。

在你的 index.php, 使用if () 條件句來有條件的輸出附加內(nèi)容:

<?php get_header(); ?>
<?php if (is_home()) {
 // we're on the home page, so let's show a picture of our new kitten!
 echo "<img src='/images/new_kitty.jpg' alt='Our new cat, Rufus!' />";
 // and now back to our regularly scheduled home page
} ?> 

如果訪問者請求某個具體文章,頁面,分類或者日期時,函數(shù)is_home()不會產(chǎn)生真值,這樣它只顯示在"主"頁上。

參見創(chuàng)建一個靜態(tài)首頁以獲得更多信息。

只顯示摘錄

顯示摘錄而不是全文的最簡單的方法,把所有的the_content()實例用the_excerpt()代替,如果你沒有建立你的文章的外部摘錄,這個函數(shù)將自動的顯示文章的前120個詞。

<div class="entry">
<?php the_excerpt(); ?>
</div>

顯示摘錄或者全部文章取決于文章編號

某些情況下,如在文檔頁面上,如果只有一篇文章的話你可能想顯示全部的內(nèi)容,或者有多篇文章顯示摘錄。你可以自定義Loop來實現(xiàn)。

<?php if (have_posts()) : ?>

  <?php if (($wp_query->post_count) > 1) : ?>
     <?php while (have_posts()) : the_post(); ?>
       <!-- Do your post header stuff here for excerpts-->
          <?php the_excerpt() ?>
       <!-- Do your post footer stuff here for excerpts-->
     <?php endwhile; ?>

  <?php else : ?>

     <?php while (have_posts()) : the_post(); ?>
       <!-- Do your post header stuff here for single post-->
          <?php the_content() ?>
       <!-- Do your post footer stuff here for single post-->
     <?php endwhile; ?>

  <?php endif; ?>

<?php else : ?>
     <!-- Stuff to do if there are no posts-->

<?php endif; ?>

不同的頁眉/邊欄/頁腳

WordPress在模板文件中提供get_header()get_sidebar(), 和get_footer() 幾個包含標簽使用。這些函數(shù)讓定義標準的頁眉/邊欄/頁腳更加簡單。任何對這些文件的改動會立刻顯示給訪問者,你不用做任何工作。

有時你可能不要邊欄。如果你不想要邊欄,只需簡單的把get_sidebar()函數(shù)的調(diào)用從模板中刪除。如,WordPress 默認主題中的模板不包括single.php。

建立你自己的不同的 邊欄,你有兩種選擇。

  1. 把邊欄內(nèi)容直接包含到你正在操作的模板文件中。如果你想要category-3 擁有不同的邊欄, 編輯category-3.php,把必須的HTML和PHP代碼包含進去來生成這個唯一的邊欄。
  2. 使用PHP的包含 函數(shù), 來包含另外一個文件。 WordPress get_sidebar() 函數(shù) 加載sidebar.php. 如果你有個文件名為sideleft.php, 你可以象這樣把它涵蓋進去::
<?php include(TEMPLATEPATH . '/sideleft.php'); ?>

使用WordPress默認的模板層次,如果你想在多個或者不同的模板中使用相同的元素,你最好把它們放到分開的模板文件中,并使用PHPinclude()函數(shù)。如果這個你添加的元素是特別為某一模板使用的,最好是把它直接包含到那個模板中去。

概要

我們剛才簡單說了下我們使用Loop能做些什么。作為提醒,下邊的內(nèi)容可能會幫助你自定義你自己的WordPress Loop。


資源