WIKI使用導(dǎo)航
站長百科導(dǎo)航
站長專題
- 網(wǎng)站推廣
- 網(wǎng)站程序
- 網(wǎng)站賺錢
- 虛擬主機(jī)
- cPanel
- 網(wǎng)址導(dǎo)航專題
- 云計算
- 微博營銷
- 虛擬主機(jī)管理系統(tǒng)
- 開放平臺
- WIKI程序與應(yīng)用
- 美國十大主機(jī)
WordPress:The Loop in Action
Introduction
"The Loop" is a term that refers to the main process of WordPress. You use The Loop in your template files to show posts to visitors. You could make templates without The Loop, but you'd only be able to display data from one post.
介紹
"The Loop"是指WordPress主程序中的一個術(shù)語。你可以在你的模板文件中使用Loop來想訪問者顯示你的文章。你可以讓模板不帶有Loop,但這樣你就只能顯示來自一篇文章的數(shù)據(jù)了。
The first thing WordPress does is check that all the files it needs are present. Next, it collects the default settings, as defined by the blog administrator, from the database. This includes things like the number of posts to display per page, whether commenting is enabled, and the like. Once these defaults are established, WordPress checks to see what the user asked for. This information is used to determine which posts to fetch from the database.
WordPress所做的第一件事就是檢查它所需要的所有文件是否存在。接下來,它按照blog 管理員定義的,從數(shù)據(jù)庫收集默認(rèn)的設(shè)置。這包括每頁顯示的文章的數(shù)量,是否允許評論,等等。一旦這些默認(rèn)的內(nèi)容確定后,WordPress就會檢查使用者要求什么。這個信息用來確定從數(shù)據(jù)庫中取出哪篇文章。s
If the user didn't ask for a specific post, category, page, or date, WordPress uses the previously collected default values to determine which posts to prepare for the user. For example, if the blog administrator has selected to display 5 posts per page in Administration > Settings > Reading, then WordPress will fetch the five most recent posts from the database. If the user did ask for a specific post, category, page, or date, then WordPress will use that information to specify which post(s) to fetch from the database.
如果使用者不要求特定的文章,分類,頁面或者日期,WordPress使用以前收集的默認(rèn)值確定哪個文章準(zhǔn)備給使用者閱讀。例如,如果blog管理員在Administration > Settings > Reading設(shè)置中選擇每頁顯示5篇文章,那么WordPress就會從數(shù)據(jù)庫中取最新的5篇。如果使用者要求特定的文章,分類,頁面或者日期,那么WordPress就會根據(jù)這些信息來選擇從數(shù)據(jù)庫中取出哪篇文章。
Once all this is done, WordPress connects to the database, retrieves the specified information, and stores the results in a variable. It is The Loop that accesses this variable, and uses the values for display in your templates.
一旦所有這些完成之后,WordPress就連接數(shù)據(jù)庫,得到需要的信息,然后在一個變量中儲存這個結(jié)果。進(jìn)入這個變量的就是Loop,然后在模板中顯示變量的值。
By default, if the visitor did not select a specific post, page, category, or date, WordPress uses index.php to display everything. For the first part of this discussion of The Loop we'll focus only on index.php, and the default display of your blog. Later on, once you understand how things work, we'll investigate tricks with The Loop in other template files.
默認(rèn)情況下,如果訪問者沒有選擇特定的文章,頁面,分類或者日期,WordPress會使用index.php顯示所有內(nèi)容。在這個Loop的討論的第一部分,我們只集中討論index.php,和默認(rèn)的blog顯示。接下來,一旦你懂得這些如何工作的時候,我們來研究Loop在別的模板文件中的作用。
The World's Simplest Index Page
The following is a fully functional index which will display the contents (and just the contents) of each post, according to the conditions used to prepare The Loop. The only purpose for showing you this is to demonstrate how little is actually necessary for the functioning of The Loop. The bulk of the stuff in your index.php is CSS, HTML, and PHP declarations to make The Loop look pretty.
<?php get_header(); if (have_posts()) : while (have_posts()) : the_post(); the_content(); endwhile; endif; get_sidebar(); get_footer(); ?>
Now, let's look at the bulk of the stuff that makes The Loop look pretty.
世界上最簡單的索引頁面
接下來的是具有所有功能的索引,可以顯示每個文章的內(nèi)容(僅僅是內(nèi)容),根據(jù)使用情況準(zhǔn)備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(); ?>
The Default Loop
The following is a step-by-step look at the default usage of the Loop that comes with the default and classic theme in the standard installation of WordPress v1.5.
默認(rèn) Loop
接下來是一步一步的來看WordPress v1.5標(biāo)準(zhǔn)安裝,默認(rèn)情況下,默認(rèn) 和 經(jīng)典主題中l(wèi)oop用法。
Begin The Loop
Found at the top of the default index.php template file is the starting code for WordPress:The Loop.
<?php if (have_posts()) : ?><br /> <?php while (have_posts()) : the_post(); ?>
- First it checks whether any posts were collected with the have_posts() function.
- If there are any posts, a PHP while loop is started. A while loop will continue to execute as long as the condition in the parenthesis is logically true. So as long as the function have_posts() returns a true value, The Loop will keep going.
- The function have_posts() simply checks the next item in the collection of posts: if there's another item, return true; if there is no next item, return false.
Loop開始
在默認(rèn)index.php模板文件的頂部,是Loop代碼開始的地方。
<?php if (have_posts()) : ?><br /> <?php while (have_posts()) : the_post(); ?>
- 首先它用have_posts()功能檢查是否有新的文章.
- 如果有文章的話,一個PHP while的loop就開始了。只要插入語為邏輯真,while loop就會繼續(xù)執(zhí)行。這樣只要函數(shù)have_posts() 返回真值,Loop就會繼續(xù)。
- 函數(shù)have_posts()簡單的在文章集合中檢查下一個項目:如果有另外一個項目,返回true,如果沒有下一個項目,返回false .
Generating the Post
The function the_post() takes the current item in the collection of posts and makes it available for use inside this iteration of The Loop. Without the_post(), many of the WordPress:Template Tags used in your theme would not work.
Once the post data is made available, the template can start showing post data to the visitor.
生成文章
the_post()函數(shù)把文章集合中的現(xiàn)用的項目拿出來,并且讓它可以在Loop的循環(huán)中使用。如果沒有the_post(),很多主題中使用的模板標(biāo)簽都無法工作了。
Title, Date and Author
The following template tags get the current post's title, as well as the time it was posted and who posted it.
<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>
標(biāo)題,日期和作者
下邊的 模板標(biāo)簽獲得當(dāng)前文章的標(biāo)題,還有發(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>
Post Content
The the_content() template tag displays the content of the post. This is the meat and potatoes of each pass through The Loop:
<div class="entry"> <?php the_content('Read the rest of this entry »'); ?> </div>
If you include the Quicktag button called more, and shown as <!--more-->, in the body of your post, only the portion above that line will be displayed to viewers. So, if you only want your front page to show the first sentence or two of every post, simply insert <!--more--> after the first line into every post you make.
文章內(nèi)容
the_content()模板標(biāo)簽顯示文章的內(nèi)容。 這是每個通過Loop的很重要的部分:
<div class="entry"> <?php the_content('Read the rest of this entry »'); ?> </div>
如果你把more這個按鈕包含到Quicktag,然后象這樣<!--more-->顯示出來, 在你的文章的正文部分,只有above這部分才會顯示給訪問者。這樣,如果你只是想讓你的首頁面顯示每篇文章的第一句或者前兩句話的話,只需要簡單的在每篇文章的第一行之后插入 <!--more-->就可以了。
When viewing a single post, the <!-- more --> delimiter is skipped. So putting the <!-- more --> delimiter into all your posts forces readers to click through to each individual post if they want to read the whole thing.
當(dāng)查看某個單一的文章的時候,<!-- more -->分隔符會被忽略。這樣如果讀者想閱讀全部內(nèi)容,而又在所有的文章中都加如<!-- more -->分隔符的話,會迫使讀者點(diǎn)擊每個單獨(dú)的文章。
Additional Details
Beneath each post's content in the index.php template file is a place to display more information about the post, such as the categories, date, and comment information. Known as the post meta data section, if you're a logged in user of sufficient privilege (or the post's author), you will also see an "Edit This" link, thanks to the edit_post_link() template tag function.
<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>
詳細(xì)資料附
在每個文章內(nèi)容下邊,在index.php模板文件中,是顯示有關(guān)這個文章更多信息的地方,如分類,日期和評論信息。大家知道的文章meta數(shù)據(jù)部分,如果你是一個已經(jīng)登陸的有充分權(quán)限的使用者,你可會看見"Edit This"連接,這多虧了edit_post_link()模板標(biāo)簽函數(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>
If commenting is enabled, or if the post has comments, the comments_popup_link() template tag will display a link to the comments. If you're using the comments popup window, this link will open the comments window; otherwise it will jump right to this post's comments.
If the visitor is viewing an index of posts (i.e.: more than one post in The Loop), the comments_popup_link() link will take the reader to this post's individual page.
如果評論被激活,或者是該文章有評論內(nèi)容,comments_popup_link()模板標(biāo)簽將回顯示一個到評論內(nèi)容的連接。如果你使用了評論彈出窗口,這個連接就會打開評論窗口,否則它就會直接跳轉(zhuǎn)到這篇文章的評論內(nèi)容。
如果訪問者在瀏覽文章索引(i.e.: 在Loop中不止一篇文章),comments_popup_link()連接將會把讀者連接到這篇文章的單獨(dú)頁面。
Trackback Autodiscovery
The trackback_rdf template tag's function is to output machine-readable code used for trackback auto-discovery.
<!-- <?php trackback_rdf(); ?> -->
Note: The trackback_rdf() tag is supposed to be used with comments around it. It is not "turned off".
自動尋找Trackback
trackback_rdf模板標(biāo)簽的功能是輸出機(jī)讀代碼用于自動尋找trackback。
<!-- <?php trackback_rdf(); ?> -->
注意:trackback_rdf()標(biāo)簽支持在自身旁邊使用評論。它并非"關(guān)閉"的。
Ending The Loop
The following ends The Loop. After this, the various post-related template tags will not work as expected (or if they do, they will use the last post from The Loop). This means, that if you need to use a template tag that works within The Loop, you need to put it in before this point.
<?php endwhile; ?>
結(jié)束 Loop
下面是結(jié)束 Loop。在這之后,各種文章相關(guān)的模板標(biāo)簽不再如如你想要的那樣工作了(如果它們在工作,它們會使用Loop的最后一篇文章).這意味著,如果你需要使用一個工作在 Loop內(nèi)的模板標(biāo)簽,你需要把如下語句放進(jìn)去。
<?php endwhile; ?>
This section, immediately after the end of The Loop, displays navigation controls to move forward and backward by each web page.
<div class="navigation"> <div class="alignleft"><?php posts_nav_link('','','« Previous Entries') ?></div> <div class="alignright"><?php posts_nav_link('','Next Entries »','') ?></div> </div>
在這個部分,在Loop結(jié)束后立即通過每個網(wǎng)頁顯示出向前還是向后的導(dǎo)航控制。
<div class="navigation"> <div class="alignleft"><?php posts_nav_link('','','« Previous Entries') ?></div> <div class="alignright"><?php posts_nav_link('','Next Entries »','') ?></div> </div>
If the blog is set to display 10 posts per page, and the conditions used by The Loop collect 25 posts, there will be three pages to navigate: two pages of 10 posts each, and one page of 5 posts. The navigation links will allow the visitor to move forward and backward through the collection of posts.
如果blog設(shè)置成每頁顯示10篇文章,而且Loop收集到了25篇文章,這就會產(chǎn)生三個頁面:兩個10篇文章的頁面,還有一個五篇的頁面。導(dǎo)航連接允許訪問者通過文章收集跳轉(zhuǎn)到上一還是下一頁。
The navigation controls are included outside The Loop, but inside the if condition, so that they only show up if there are any posts. The navigation functions themselves also check whether or not there is anything to which they will link, based on the current Loop, and only display links if there's something to link.
<?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>
The else : clause determines what to do if have_posts() (from way up at the top) is false. That is to say, the stuff after the else will only be executed/displayed if The Loop had zero posts. No posts show up if, for example, the visitor requested a specific day for which no posts were made or a search was performed that produced no results.
<?php endif; ?>
This ends the conditional test of "if there are posts do this, else if there are no posts, do that". Once the conditional test is finished, the default index.php template next includes the sidebar, and finally the footer.
導(dǎo)航控制是不包含在Loop內(nèi)的,但是包含在if 條件句內(nèi),這樣它們只能顯示是否有文章。導(dǎo)航函數(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é)束了,默認(rèn)的index.php模板包括了邊欄,最后是頁腳。
The Loop In Other Templates
WordPress can use different template files for displaying your blog in different ways. In the default WordPress theme, there are template files for the index view, category view, and archive view, as well as a template for viewing individual posts. Each of these uses WordPress:The Loop, but does so with slightly different formatting, as well as different uses of the template tags.
其它模板中的Loop
WordPress可以使用不同的模板文件,用不同的方式顯示你的blog。在默認(rèn)WordPress主題中,有用于索引瀏覽的模板文件,分類瀏覽和文檔瀏覽,就象一個瀏覽單獨(dú)文章的模板。這些都會用到Loop,但是這些都沒有太大的差距,就象模板標(biāo)簽之間的不同用法似的。
For any view which does not have a separate template file, WordPress will use index.php by default. If a visitor requests a single post, WordPress will first look for a file named single.php. If that file exists, it will be used to present the post to the visitor. If that file does not exist, WordPress will use index.php to present the post to the visitor. This is called the WordPress:Template Hierarchy.
至于那些沒有分開的模板文件的情況,WordPress使用默認(rèn)時的index.php。如果訪問者請求閱讀一個單獨(dú)的文章時,WordPress會首先查找一個名字為single.php的文件。如果這個文件存在,它就會用來顯示這個文章。如果不存在,WordPress就會使用index.php來顯示文章。這叫做模板層次.
If you are making your own Theme, it's often helpful to look at the template files from the default Theme as a point of reference. It's also helpful to use your theme's index.php as a template for your other template files. Doing so may give you a known and working page from which to begin making changes as you create more template files.
如果你正在制作你自己的主題,在默認(rèn)主題中查看模板文件,作為參考,很有用。同樣,使用你的主題的index.php作為你的其他模板文件的模板也是很有用的。因為你創(chuàng)建了更多的模板文件,這樣做可能會帶給你一個已知的工作頁面,從這里開始作出更改。
Different Archive Format
An archive is a collection of historical posts. In the default usage, the posts displayed on your main index are recent chronological postings. When a visitor clicks on one of your archive links, or if they manually request a specific date (http://www.example.com/blog/index.php?m=200504 or http://www.example.com/blog/2005/04 to select all posts from April, 2005), WordPress will display an archive view. By default, the archive will use index.php, and thus look the same as your front page, just displaying the posts from April 2005.
不同的文檔格式
archive是歷史文章的集合。在默認(rèn)時,文章在主索引中顯示的是最新的按時間順序的記錄。當(dāng)訪問者點(diǎn)擊某個文檔連接時,或者他們手動請求某個特定時間時,(使用http://www.example.com/blog/index.php?m=200504或者 http://www.example.com/blog/2005/04 來選擇所有2005年4月后的文章),WordPress將顯示archive內(nèi)容。默認(rèn)情況下,文檔將使用index.php,然后和你的首頁一樣,只顯示出2005年四月后的文章。
When WordPress prepares an archive view for a visitor, it specifically looks for a file named archive.php in your current theme's directory. If you'd like to visually disambiguate archives from your front page, simply copy index.php to archive.php, and edit archive.php as necessary!
當(dāng)WordPress為訪問者準(zhǔn)備文檔界面時,它會在你現(xiàn)用的主題目錄中明確的尋找一個叫做archive.php的文件,如果你想在首頁上使文檔的意思明確表達(dá),那么把index.php復(fù)制到archive.php,并且按需求編輯archive.php文件。
For example, if you want to show only post titles, and no post content, for your list of archives, you could use something like this:
例如,如果你只想在文檔列表上顯示文章標(biāo)題,不包含文章內(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('','','« Previous Entries') ?> </div> <div class="alignright"> <?php posts_nav_link('','Next Entries »','') ?> </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(); ?>
Different Category Format
Like the archive views, WordPress looks for a separate template file for category views. If a visitor clicks on a link for a category in your blog, they will be taken to the category view. WordPress will prepare The Loop with posts from that category only, limiting the number of posts per the blog's default settings.
不同的分類格式
和文檔界面一樣,WordPress為分類界面尋找分開的模板文件。如果訪問者點(diǎn)擊了一個分類連接,他們會看到分類外觀,WordPress會只從分類中準(zhǔn)備帶有文章的Loop,限制每個blog默認(rèn)設(shè)置下的文章的數(shù)目。
To make your category view different from your index view, copy index.php and rename it category.php. For a category view, it's probably not necessary to list the categories to which a post is assigned, so let's remove that portion. Instead, let's announce the category at the top of the page:
要想讓你的分類界面和索引界面不同的話,復(fù)制一個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('','','« Previous Entries') ?> </div> <div class="alignright"> <?php posts_nav_link('','Next Entries »','') ?> </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(); ?>
Different Formats for Different Categories
As explained in the WordPress:Template Hierarchy, it is possible to create separate template files for each category. Simply name the file category-X.php, where X is the numerical ID of the category. Consider carefully whether you need a whole new template for a specific category.
不同分類的不同格式
如在模板層次中敘述的,為每個分類創(chuàng)建分開的模板文件是可行的。只需要建立名字為category-X.php的文件,這里X是用數(shù)字表示的分類。仔細(xì)考慮你是否需要給某個分類建立全新的完整模板。
Let's look at two categories, "Plants" and "Flowers", with category IDs 3 and 4, respectively. Next to each post title in the output you want to have picture of either a plant, or a flower, depending on which category is being displayed. You could:
- Use two separate files, category-3.php and category-4.php, each with a different img tag for each post title.
- Use a conditional test inside your default category.php file to check whether the current category is "Plants" or "Flowers" (or neither), and display the appropriate image:
<?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 ?>
讓我們來看兩個分類,"Plants" 和 "Flowers",分類ID分別為3和4。在輸出的地方的每個文章標(biāo)題旁邊,你都想要有個植物或者花的圖片,取決于哪個分類被顯示,你可以這樣:
- 使用兩個分開的文件, category-3.php 和 category-4.php, 每個文件給每個文章標(biāo)題使用不同的img標(biāo)簽。
- 在你的默認(rèn)category.php文件中使用條件判斷,來查看是否當(dāng)前分類是"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 ?>
If you added another category, "Cars", which you wanted to display in a significantly different way, then a separate category-X.php would be more appropriate.
如果你添加了另外一個分類,"Cars",你想讓它用一個引人注目的方式顯示出來,那么一個分開的category-X.php則是你最合適的選擇。
Different CSS For Different Categories
Many users want to create separate CSS files for a specific category. This, too, can be easily accomplished. It is important to remember that stylesheets are defined and loaded in the <head> section of the HTML document. WordPress uses the header.php file for this. In the default header.php, find this line:
<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 } ?>
Note: The Cars template uses the category-5.css file to override the default layout. In this example the CSS file is named after the category template file to which it will be applied, rather than the actual name of the category. Thus, you know that category-5.css goes with category-5.php.
不同分類的不同CSS
很多使用者想給特定的分類建立單獨(dú)的CSS文件。這很容易實現(xiàn)。記住樣式表是在HTML文件中的<head>部分定義并且加載的。WordPress使用header.php文件,在默認(rèn)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文件取代默認(rèn)版面。在這個例子中,CSS文件在應(yīng)用的分類模板文件之后被命名,而不是真正的分類名。這樣你就明白 category-5.css 是和category-5.php文件聯(lián)系在一起的了.
Different Single Post Format
When viewing any single post (or permalink), WordPress will use single.php, if present.
This portion, from the WordPress default single.php, provides the post meta data information about the current post:
不同的單一文章格式
當(dāng)瀏覽任意單一的文章(或者 permalink)的時候,WordPress會使用single.php。如果有的話。
在這個部分,在WordPress默認(rèn)的single.php文件中,提供了有關(guān)當(dāng)前文章的文章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>
This sort of information -- whether comments are open or closed -- is largely inappropriate on an index, archive, or category view; which is why it's only included in the single.php template file.
這種信息—不論評論是公開還是關(guān)閉—是相當(dāng)不適合放在一個索引,文檔或者分類界面的;那也是為什么只包含在single.php模板文件中的原因。
Other Loop Tricks
Now that you have a good introduction to the basic uses for the WordPress Loop, let's introduce you to some more Loop effects and tricks.
其他Loop技巧
現(xiàn)在你已經(jīng)對WordPress Loop的基本使用有了一個全面的了解,我們開始介紹更多的Loop效果和技巧。
Static Front Page
How can you display something special only on the front page of your blog? That's right, only on the front page or home page, and have it not be seen anywhere else on your site. Easy! We call this the static front page. The front or first page of your site isn't really static. It's just using the Loop to make it look that way.
To make this Loop trick work, use the is_home() conditional template tag function.
靜態(tài)首頁
你如何顯示一些blog中特別的僅僅存在于你的首頁上的東西呢?對,就是只在你的首頁或者主頁上的,站點(diǎn)上別的任何地方都看不到的。簡單!我們把這稱為靜態(tài)首頁。站點(diǎn)的首頁并不真的是靜態(tài)的。它只是使用了Loop讓它看起來如此罷了。
想讓這個Loop技巧實現(xiàn),使用is_home()條件式模板標(biāo)簽函數(shù)。
In your index.php, use an if () test to conditionally output additional content:
在你的 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 } ?>
The function is_home() will only produce a true value if the visitor is not requesting a specific post, page, category, or date, so it only shows up on the "home" page.
如果訪問者請求某個特殊文章,頁面,分類或者日期時,函數(shù)is_home()不會產(chǎn)生真值,這樣它只顯示在"主"頁上。
For more information, see WordPress:Creating a Static Front Page.
參見創(chuàng)建一個靜態(tài)首頁以獲得更多信息。
Excerpts Only
The easiest way to display excerpts, instead of the full content, of posts, replace all instances of the_content() with the_excerpt(). If you have not created explicit excerpts for your posts, this function will automatically display the first 120 words of the post.
<div class="entry"> <?php the_excerpt(); ?> </div>
只顯示摘錄
顯示摘錄而不是全文的最簡單的方法,把所有的the_content()實例用the_excerpt()代替,如果你沒有建立你的文章的外部摘錄,這個函數(shù)將自動的顯示文章的前120個詞。
<div class="entry"> <?php the_excerpt(); ?> </div>
Showing Excerpts or Full Post Depending Upon Number of Posts
In some circumstances, for example on archive pages, you may want to show the full post if there is only one post or excerpts if there are multiple posts. You can customize the loop to do this.
顯示摘錄或者全部文章取決于文章編號
某些情況下,如在文檔頁面上,如果只有一篇文章的話你可能想顯示全部的內(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; ?>
Different Headers/Sidebars/Footers
WordPress offers the get_header(), get_sidebar(), and get_footer() WordPress:Include Tags for use in your 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在模板文件中提供get_header(),get_sidebar(), 和get_footer() 幾個包含標(biāo)簽使用。這些函數(shù)讓定義標(biāo)準(zhǔn)的頁眉/邊欄/頁腳更加簡單。任何對這些文件的改動會立刻顯示給訪問者,你不用做任何工作。
But sometimes you might not want a sidebar. If you don't want a sidebar, simply exclude the call to the get_sidebar() function from your template. For example, the single.php template in the WordPress default theme does not include a sidebar.
有時你可能不想要邊欄。如果你不想要邊欄,只需簡單的把get_sidebar()函數(shù)的調(diào)用從模板中刪除。如,WordPress 默認(rèn)主題中的模板不包括single.php。
To create your own different sidebar, you have two choices.
- 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 category-3.php and include the necessary HTML and PHP to generate your distinctive sidebar.
- Use the PHP include function, to include another file. The WordPress get_sidebar() function only loads sidebar.php. If you make a file named sideleft.php, you would include it like this:
<?php include(TEMPLATEPATH . '/sideleft.php'); ?>
眼建立你自己的不同的 邊欄,你有兩種選擇。
- 把邊欄內(nèi)容直接包含到你正在操作的模板文件中。如果你想要category-3 擁有不同的邊欄, 編輯category-3.php,把必須的HTML和PHP代碼包含進(jìn)去來生成這個唯一的邊欄。
- 使用PHP的包含 函數(shù), 來包含另外一個文件。 WordPress get_sidebar() 函數(shù) 只 加載sidebar.php. 如果你有個文件名為sideleft.php, 你可以象這樣把它涵蓋進(jìn)去::
<?php include(TEMPLATEPATH . '/sideleft.php'); ?>
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 include() 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默認(rèn)的模板層次,如果你想在多個或者不同的模板中使用相同的元素,你最好把它們放到分開的模板文件中,并使用PHPinclude()函數(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 Loop.
概要
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 Loop.
我們剛才簡單說了下我們使用Loop能做些什么。作為提醒,下邊的內(nèi)容可能會幫助你自定義你自己的WordPress Loop。