久久精品水蜜桃av综合天堂,久久精品丝袜高跟鞋,精品国产肉丝袜久久,国产一区二区三区色噜噜,黑人video粗暴亚裔
站長百科 | 數(shù)字化技能提升教程 數(shù)字化時代生存寶典
首頁
數(shù)字化百科
電子書
建站程序
開發(fā)
服務(wù)器
辦公軟件
開發(fā)教程
服務(wù)器教程
軟件使用教程
運營教程
熱門電子書
WordPress教程
寶塔面板教程
CSS教程
Shopify教程
導航
程序頻道
推廣頻道
網(wǎng)賺頻道
人物頻道
網(wǎng)站程序
網(wǎng)頁制作
云計算
服務(wù)器
CMS
論壇
網(wǎng)店
虛擬主機
cPanel
網(wǎng)址導航
WIKI使用導航
WIKI首頁
最新資訊
網(wǎng)站程序
站長人物
頁面分類
使用幫助
編輯測試
創(chuàng)建條目
網(wǎng)站地圖
站長百科導航
站長百科
主機偵探
IDCtalk云說
跨境電商導航
WordPress啦
站長專題
網(wǎng)站推廣
網(wǎng)站程序
網(wǎng)站賺錢
虛擬主機
cPanel
網(wǎng)址導航專題
云計算
微博營銷
虛擬主機管理系統(tǒng)
開放平臺
WIKI程序與應用
美國十大主機
編輯“
WordPress:The Loop in Action
”
人物百科
|
營銷百科
|
網(wǎng)賺百科
|
站長工具
|
網(wǎng)站程序
|
域名主機
|
互聯(lián)網(wǎng)公司
|
分類索引
Seadragon530
(
討論
|
貢獻
)
2008年4月18日 (五) 16:53的版本
(
差異
)
←上一版本
|
最后版本
(
差異
) |
下一版本→
(
差異
)
跳轉(zhuǎn)至:
導航
、?
搜索
警告:您正在編輯的是本頁面的舊版本。
如果您發(fā)布該更改,該版本后的所有更改都會丟失。
警告:
您沒有登錄。如果您做出任意編輯,您的IP地址將會公開可見。如果您
登錄
或
創(chuàng)建
一個賬戶,您的編輯將歸屬于您的用戶名,且將享受其他好處。
反垃圾檢查。
不要
加入這個!
==Introduction== [[WordPress:The Loop|"The Loop"]] is a term that refers to the main process of WordPress. You use The Loop in your [[WordPress:Templates|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. ==介紹== [[WordPress:The Loop|"The Loop"]]是指WordPress主程序中的一個術(shù)語。你可以在你的[[WordPress:Templates|模板文件]]中使用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 [[WordPress:Registered_User_Features|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所做的第一件事就是檢查它所需要的所有文件是否存在。接下來,它按照[[WordPress:Registered_User_Features|blog 管理員]]定義的,從數(shù)據(jù)庫收集默認的設(shè)置。這包括每頁顯示的文章的數(shù)量,是否允許評論,等等。一旦這些默認的內(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 [[WordPress:Administration_Panels|Administration]] > [[WordPress:Administration_Panels#Reading|Settings]] > [[WordPress:Settings_Reading_SubPanel|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使用以前收集的默認值確定哪個文章準備給使用者閱讀。例如,如果blog管理員在[[WordPress:Administration_Panels|Administration]] > [[WordPress:Administration_Panels#Reading|Settings]] > [[WordPress:Settings_Reading_SubPanel|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é)果。進入這個變量的就是Loop,然后在模板中顯示變量的值。 By default, if the visitor did not select a specific post, page, category, or date, WordPress uses <tt>index.php</tt> to display everything. For the first part of this discussion of The Loop we'll focus only on <tt>index.php</tt>, 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. 默認情況下,如果訪問者沒有選擇特定的文章,頁面,分類或者日期,WordPress會使用<tt>index.php</tt>顯示所有內(nèi)容。在這個Loop的討論的第一部分,我們只集中討論<tt>index.php</tt>,和默認的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 <tt>index.php</tt> is CSS, HTML, and PHP declarations to make The Loop look pretty. <pre> <?php get_header(); if (have_posts()) : while (have_posts()) : the_post(); the_content(); endwhile; endif; get_sidebar(); get_footer(); ?> </pre> Now, let's look at the bulk of the stuff that makes The Loop look pretty. ==世界上最簡單的索引頁面== 接下來的是具有所有功能的索引,可以顯示每個文章的內(nèi)容(僅僅是內(nèi)容),根據(jù)使用情況準備Loop,給你看這些的唯一目的就是表明這些對Loop的技能幾乎沒有必要,在你的<tt>index.php</tt>文件大多數(shù)內(nèi)容中,都是CSS, HTML,和 PHP聲明,可以讓Loop看起來漂亮些。 <pre> <?php get_header(); if (have_posts()) : while (have_posts()) : the_post(); the_content(); endwhile; endif; get_sidebar(); get_footer(); ?> </pre> ==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. ==默認 Loop== 接下來是一步一步的來看WordPress v1.5標準安裝,默認情況下,''默認'' 和 ''經(jīng)典''主題中l(wèi)oop用法。 ===Begin The Loop=== Found at the top of the default <tt>index.php</tt> template file is the starting code for [[WordPress:The Loop]]. <pre><?php if (have_posts()) : ?><br /> <?php while (have_posts()) : the_post(); ?></pre> #First it checks whether any posts were collected with the <tt>have_posts()</tt> function. #If there are any posts, a PHP <tt>[http://www.php.net/while while]</tt> loop is started. A <tt>while</tt> loop will continue to execute as long as the condition in the parenthesis is logically true. So as long as the function <tt>have_posts()</tt> returns a true value, The Loop will keep going. #The function <tt>have_posts()</tt> 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開始=== 在默認<tt>index.php</tt>模板文件的頂部,是[[WordPress:The Loop|Loop]]代碼開始的地方。 <pre><?php if (have_posts()) : ?><br /> <?php while (have_posts()) : the_post(); ?></pre> #首先它用<tt>have_posts()</tt>功能檢查是否有新的文章. #如果有文章的話,一個PHP <tt>[http://www.php.net/while while]</tt>的loop就開始了。只要插入語為邏輯真,<tt>while</tt> loop就會繼續(xù)執(zhí)行。這樣只要函數(shù)<tt>have_posts()</tt> 返回真值,Loop就會繼續(xù)。 #函數(shù)<tt>have_posts()</tt>簡單的在文章集合中檢查下一個項目:如果有另外一個項目,返回true,如果沒有下一個項目,返回false . ===Generating the Post=== The function <tt>the_post()</tt> takes the current item in the collection of posts and makes it available for use inside this iteration of The Loop. Without <tt>the_post()</tt>, 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. ===生成文章=== <tt>the_post()</tt>函數(shù)把文章集合中的現(xiàn)用的項目拿出來,并且讓它可以在Loop的循環(huán)中使用。如果沒有<tt>the_post()</tt>,很多主題中使用的[[WordPress:Template Tags|模板標簽]]都無法工作了。 ====Title, Date and Author==== The following [[WordPress:Template Tags|template tags]] get the current post's title, as well as the time it was posted and who posted it. <pre> <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> </pre> ====標題,日期和作者==== 下邊的 [[WordPress:Template Tags|模板標簽]]獲得當前文章的標題,還有發(fā)表的時間和誰發(fā)表了它。 <pre> <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> </pre> ====Post Content==== The <tt>[[WordPress:Template_Tags/the_content|the_content()]]</tt> template tag displays the content of the post. This is the meat and potatoes of each pass through The Loop: <pre> <div class="entry"> <?php the_content('Read the rest of this entry &raquo;'); ?> </div> </pre> If you include the [[WordPress:Write_Post_SubPanel#Quicktags|Quicktag]] button called '''more''', and shown as <tt>[[WordPress:Customizing_the_Read_More|<!--more-->]]</tt>, in the body of your post, only the portion <em>above</em> 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 <tt><!--more--></tt> after the first line into every post you make. ====文章內(nèi)容==== <tt>[[WordPress:Template_Tags/the_content|the_content()]]</tt>模板標簽顯示文章的內(nèi)容。 這是每個通過Loop的很重要的部分: <pre> <div class="entry"> <?php the_content('Read the rest of this entry &raquo;'); ?> </div> </pre> 如果你把'''more'''這個按鈕包含到[[WordPress:Write_Post_SubPanel#Quicktags|Quicktag]],然后象這樣<tt>[[WordPress:Customizing_the_Read_More|<!--more-->]]</tt>顯示出來, 在你的文章的正文部分,只有<em>above</em>這部分才會顯示給訪問者。這樣,如果你只是想讓你的首頁面顯示每篇文章的第一句或者前兩句話的話,只需要簡單的在每篇文章的第一行之后插入 <tt><!--more--></tt>就可以了。 When viewing a single post, the <tt><!-- more --></tt> delimiter is skipped. So putting the <tt><!-- more --></tt> delimiter into all your posts forces readers to click through to each individual post if they want to read the whole thing. 當查看某個單一的文章的時候,<tt><!-- more --></tt>分隔符會被忽略。這樣如果讀者想閱讀全部內(nèi)容,而又在所有的文章中都加如<tt><!-- more --></tt>分隔符的話,會迫使讀者點擊每個單獨的文章。 ====Additional Details==== Beneath each post's content in the <tt>index.php</tt> template file is a place to display more information about the post, such as the categories, date, and comment information. Known as the [[WordPress:Post_Meta_Data_Section|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 <tt>[[WordPress:Template_Tags/edit_post_link|edit_post_link()]]</tt> template tag function. <pre> <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> </pre> ====詳細資料附==== 在每個文章內(nèi)容下邊,在<tt>index.php</tt>模板文件中,是顯示有關(guān)這個文章更多信息的地方,如分類,日期和評論信息。大家知道的[[WordPress:Post_Meta_Data_Section|文章meta數(shù)據(jù)部分]],如果你是一個已經(jīng)登陸的有充分權(quán)限的使用者,你可會看見"Edit This"連接,這多虧了<tt>[[WordPress:Template_Tags/edit_post_link|edit_post_link()]]</tt>模板標簽函數(shù)。 <pre> <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> </pre> If commenting is enabled, or if the post has comments, the <tt>[[WordPress:Template_Tags/comments_popup_link|comments_popup_link()]]</tt> template tag will display a link to the comments. If you're using the [[WordPress:Template_Tags/comments_popup_script|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 <tt>comments_popup_link()</tt> link will take the reader to this post's individual page. 如果評論被激活,或者是該文章有評論內(nèi)容,<tt>[[WordPress:Template_Tags/comments_popup_link|comments_popup_link()]]</tt>模板標簽將回顯示一個到評論內(nèi)容的連接。如果你使用了[[WordPress:Template_Tags/comments_popup_script|評論彈出窗口]],這個連接就會打開評論窗口,否則它就會直接跳轉(zhuǎn)到這篇文章的評論內(nèi)容。 如果訪問者在瀏覽文章索引(''i.e.:'' 在Loop中不止一篇文章),<tt>comments_popup_link()</tt>連接將會把讀者連接到這篇文章的單獨頁面。 ====Trackback Autodiscovery==== The <tt>[[WordPress:Template_Tags/trackback_rdf|trackback_rdf]]</tt> template tag's function is to output machine-readable code used for [[WordPress:Glossary#Trackback|trackback]] auto-discovery. <pre> <!-- <?php trackback_rdf(); ?> --> </pre> '''Note:''' The <tt>trackback_rdf()</tt> tag is supposed to be used with [[WordPress:Commenting_Code|comments]] around it. It is not "turned off". ====自動尋找Trackback ==== <tt>[[WordPress:Template_Tags/trackback_rdf|trackback_rdf]]</tt>模板標簽的功能是輸出機讀代碼用于自動尋找[[WordPress:Glossary#Trackback|trackback]]。 <pre> <!-- <?php trackback_rdf(); ?> --> </pre> '''注意:'''<tt>trackback_rdf()</tt>標簽支持在自身旁邊使用[[WordPress:Commenting_Code|評論]]。它并非"關(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. <pre> <?php endwhile; ?> </pre> ===結(jié)束 Loop=== 下面是結(jié)束 Loop。在這之后,各種文章相關(guān)的模板標簽不再如如你想要的那樣工作了(如果它們在工作,它們會使用Loop的最后一篇文章).這意味著,如果你需要使用一個工作'''在 Loop內(nèi)'''的模板標簽,你需要把如下語句放進去。 <pre> <?php endwhile; ?> </pre> This section, immediately after the end of The Loop, displays navigation controls to move forward and backward by each web page. <pre> <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> </pre> 在這個部分,在Loop結(jié)束后立即通過每個網(wǎng)頁顯示出向前還是向后的導航控制。 <pre> <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> </pre> 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篇文章的頁面,還有一個五篇的頁面。導航連接允許訪問者通過文章收集跳轉(zhuǎn)到上一還是下一頁。 The navigation controls are included <em>outside</em> The Loop, but <em>inside</em> the <tt>if</tt> 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. <pre> <?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> </pre> The <tt>else :</tt> clause determines what to do if <tt>have_posts()</tt> (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. <pre> <?php endif; ?> </pre> 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. 導航控制是<em>不包含</em>在Loop內(nèi)的,但是<em>包含在</em><tt>if</tt> 條件句內(nèi),這樣它們只能顯示是否有文章。導航函數(shù)本身也會檢查是否有一些基于現(xiàn)有Loop的,它們能連接的內(nèi)容,如果有可連接的內(nèi)容的話只顯示連接。 <pre> <?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> </pre> <tt>else :</tt>語句決定了如果<tt>have_posts()</tt>返回false時做些什么。那就是說,'''else'''之后的部分只能在Loop沒有文章時才會被顯示。沒有文章顯示出來,舉例來說,訪問者要求某個特殊日子的內(nèi)容,但是那天沒有文章,或者是搜索但是沒有結(jié)果。 <pre> <?php endif; ?> </pre> 這個語句結(jié)束了這樣的條件句:"如果有文章這樣,如果沒有文章那樣".一旦條件語句結(jié)束了,默認的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 [[WordPress:Templates|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 [[WordPress:Template_Tags|template tags]]. ==其它模板中的Loop== WordPress可以使用不同的模板文件,用不同的方式顯示你的blog。在默認WordPress主題中,有用于索引瀏覽的[[WordPress:Templates|模板文件]],分類瀏覽和文檔瀏覽,就象一個瀏覽單獨文章的模板。這些都會用到[[WordPress:The Loop|Loop]],但是這些都沒有太大的差距,就象[[WordPress:Template_Tags|模板標簽]]之間的不同用法似的。 For any view which does not have a separate template file, WordPress will use <tt>index.php</tt> by default. If a visitor requests a single post, WordPress will first look for a file named <tt>single.php</tt>. If that file exists, it will be used to present the post to the visitor. If that file does not exist, WordPress will use <tt>index.php</tt> to present the post to the visitor. This is called the [[WordPress:Template Hierarchy]]. 至于那些沒有分開的模板文件的情況,WordPress使用默認時的<tt>index.php</tt>。如果訪問者請求閱讀一個單獨的文章時,WordPress會首先查找一個名字為<tt>single.php</tt>的文件。如果這個文件存在,它就會用來顯示這個文章。如果不存在,WordPress就會使用<tt>index.php</tt>來顯示文章。這叫做[[WordPress:Template Hierarchy|模板層次]]. If you are making your own [[WordPress:Using Themes|Theme]], it's often helpful to look at the [[WordPress:Templates|template files]] from the default Theme as a point of reference. It's also helpful to use your theme's <tt>index.php</tt> 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. 如果你正在制作你自己的[[WordPress:Using Themes|主題]],在默認主題中查看[[WordPress:Templates|模板文件]],作為參考,很有用。同樣,使用你的主題的<tt>index.php</tt>作為你的其他模板文件的模板也是很有用的。因為你創(chuàng)建了更多的模板文件,這樣做可能會帶給你一個已知的工作頁面,從這里開始作出更改。 ===Different Archive Format=== An <em>archive</em> is a collection of historical posts. In the default usage, the posts displayed on your main index are recent [http://mydatapages.com/chronological.html chronological] postings. When a visitor clicks on one of your archive links, or if they manually request a specific date (<nowiki>http://www.example.com/blog/index.php?m=200504</nowiki> or <nowiki>http://www.example.com/blog/2005/04</nowiki> to select all posts from April, 2005), WordPress will display an <em>archive</em> view. By default, the archive will use <tt>index.php</tt>, and thus look the same as your front page, just displaying the posts from April 2005. ===不同的文檔格式=== <em>archive</em>是歷史文章的集合。在默認時,文章在主索引中顯示的是最新的[http://mydatapages.com/chronological.html 按時間順序的]記錄。當訪問者點擊某個文檔連接時,或者他們手動請求某個特定時間時,(使用<nowiki>http://www.example.com/blog/index.php?m=200504</nowiki>或者 <nowiki>http://www.example.com/blog/2005/04</nowiki> 來選擇所有2005年4月后的文章),WordPress將顯示<em>archive</em>內(nèi)容。默認情況下,文檔將使用<tt>index.php</tt>,然后和你的首頁一樣,只顯示出2005年四月后的文章。 When WordPress prepares an [[WordPress:Creating_an_Archive_Index|archive view]] for a visitor, it specifically looks for a file named <tt>archive.php</tt> in your current theme's directory. If you'd like to visually disambiguate archives from your front page, simply copy <tt>index.php</tt> to <tt>archive.php</tt>, and edit <tt>archive.php</tt> as necessary! 當WordPress為訪問者準備[[WordPress:Creating_an_Archive_Index|文檔界面]]時,它會在你現(xiàn)用的主題目錄中明確的尋找一個叫做<tt>archive.php</tt>的文件,如果你想在首頁上使文檔的意思明確表達,那么把<tt>index.php</tt>復制到<tt>archive.php</tt>,并且按需求編輯<tt>archive.php</tt>文件。 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: 例如,如果你只想在文檔列表上顯示文章標題,不包含文章內(nèi)容,你可以使用如下代碼: <pre> <?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(); ?> </pre> ===Different Category Format=== Like the archive views, WordPress looks for a separate template file for [[WordPress:Category_Templates|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為[[WordPress:Category_Templates|分類界面]]尋找分開的模板文件。如果訪問者點擊了一個分類連接,他們會看到分類外觀,WordPress會只從分類中準備帶有文章的Loop,限制每個blog默認設(shè)置下的文章的數(shù)目。 To make your category view different from your index view, copy <tt>index.php</tt> and rename it <tt>category.php</tt>. 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: 要想讓你的分類界面和索引界面不同的話,復制一個<tt>index.php</tt>文件并命名為<tt>category.php</tt>,對于分類界面,向一個分配過的文章列出分類也許不是必須的,所以我們忽略這一步。取而代之的,我們在頁面頂部聲明分類: <pre> <?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(); ?> </pre> ===Different Formats for Different Categories=== As explained in the [[WordPress:Template Hierarchy]], it is possible to [[WordPress:Category_Templates|create separate template files for each category]]. Simply name the file <tt>category-<b><u>X</u></b>.php</tt>, where <b><u>X</u></b> is the numerical ID of the category. Consider carefully whether you need a whole new template for a specific category. ===不同分類的不同格式=== 如在[[WordPress:Template Hierarchy|模板層次]]中敘述的,[[WordPress:Category_Templates|為每個分類創(chuàng)建分開的模板文件]]是可行的。只需要建立名字為<tt>category-<b><u>X</u></b>.php</tt>的文件,這里<b><u>X</u></b>是用數(shù)字表示的分類。仔細考慮你是否需要給某個分類建立全新的完整模板。 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, <tt>category-3.php</tt> and <tt>category-4.php</tt>, each with a different <tt>img</tt> tag for each post title. * Use a conditional test inside your default <tt>category.php</tt> file to check whether the current category is "Plants" or "Flowers" (or neither), and display the appropriate image: <pre> <?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 ?> </pre> 讓我們來看兩個分類,"Plants" 和 "Flowers",分類ID分別為3和4。在輸出的地方的每個文章標題旁邊,你都想要有個植物或者花的圖片,取決于哪個分類被顯示,你可以這樣: * 使用兩個分開的文件, <tt>category-3.php</tt> 和 <tt>category-4.php</tt>, 每個文件給每個文章標題使用不同的<tt>img</tt>標簽。 * 在你的默認<tt>category.php</tt>文件中使用條件判斷,來查看是否當前分類是"Plants"或者 "Flowers" (或者都不是), 然后顯示合適的圖片: <pre> <?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 ?> </pre> If you added another category, "Cars", which you wanted to display in a <em>significantly</em> different way, then a separate <tt>category-<b><u>X</u></b>.php</tt> would be more appropriate. 如果你添加了另外一個分類,"Cars",你想讓它用一個<em>引人注目</em>的方式顯示出來,那么一個分開的<tt>category-<b><u>X</u></b>.php</tt>則是你最合適的選擇。 === 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 <tt><head></tt> section of the HTML document. WordPress uses the <tt>header.php</tt> file for this. In the default <tt>header.php</tt>, find this line: <pre> <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" /> </pre> And change it to something like this: <pre> <?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 } ?> </pre> <strong>Note:</strong> The Cars template uses the <tt>category-5.css</tt> 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 <tt>category-5.css</tt> goes with <tt>category-5.php</tt>. === 不同分類的不同CSS === 很多使用者想給特定的分類建立單獨的CSS文件。這很容易實現(xiàn)。記住樣式表是在HTML文件中的<tt><head></tt>部分定義并且加載的。WordPress使用<tt>header.php</tt>文件,在默認<tt>header.php</tt>文件中,找到下列語句: <pre> <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" /> </pre> And change it to something like this: <pre> <?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 } ?> </pre> <strong>注意:</strong> 汽車分類模板使用<tt>category-5.css</tt>文件取代默認版面。在這個例子中,CSS文件在應用的分類模板文件之后被命名,而不是真正的分類名。這樣你就明白 <tt>category-5.css</tt> 是和<tt>category-5.php</tt>文件聯(lián)系在一起的了. ===Different Single Post Format=== When viewing any single post (or [[WordPress:Glossary#Permalink|permalink]]), WordPress will use <tt>single.php</tt>, if present. This portion, from the WordPress default single.php, provides the [[WordPress:Post_Meta_Data_Section|post meta data information]] about the current post: ===不同的單一文章格式=== 當瀏覽任意單一的文章(或者 [[WordPress:Glossary#Permalink|permalink]])的時候,WordPress會使用<tt>single.php</tt>。如果有的話。 在這個部分,在WordPress默認的single.php文件中,提供了有關(guān)當前文章的[[WordPress:Post_Meta_Data_Section|文章meta數(shù)據(jù)信息]]: <pre> <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> </pre> 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 <tt>single.php</tt> template file. 這種信息—不論評論是公開還是關(guān)閉—是相當不適合放在一個索引,文檔或者分類界面的;那也是為什么只包含在<tt>single.php</tt>模板文件中的原因。 ==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 <em>only</em> 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 [[WordPress:Conditional_Tags#The_Main_Page|is_home()]] conditional template tag function. ===靜態(tài)首頁=== 你如何顯示一些blog中特別的<em>僅僅</em>存在于你的首頁上的東西呢?對,就是只在你的首頁或者主頁上的,站點上別的任何地方都看不到的。簡單!我們把這稱為''靜態(tài)首頁''。站點的首頁并不真的是靜態(tài)的。它只是使用了Loop讓它看起來如此罷了。 想讓這個Loop技巧實現(xiàn),使用[[WordPress:Conditional_Tags#The_Main_Page|is_home()]]條件式模板標簽函數(shù)。 In your <tt>index.php</tt>, use an <tt>if ()</tt> test to conditionally output additional content: 在你的 <tt>index.php</tt>, 使用<tt>if ()</tt> 條件句來有條件的輸出附加內(nèi)容: <pre> <?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 } ?> </pre> The function <tt>is_home()</tt> 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ù)<tt>is_home()</tt>不會產(chǎn)生真值,這樣它只顯示在"主"頁上。 For more information, see [[WordPress:Creating a Static Front Page]]. 參見[[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 <tt>[[WordPress:Template_Tags/the_content|the_content]]()</tt> with <tt>[[WordPress:Template_Tags/the_excerpt|the_excerpt()]]</tt>. If you have not created explicit excerpts for your posts, this function will automatically display the first 120 words of the post. <pre> <div class="entry"> <?php the_excerpt(); ?> </div> </pre> ===只顯示摘錄=== 顯示摘錄而不是全文的最簡單的方法,把所有的<tt>[[WordPress:Template_Tags/the_content|the_content]]()</tt>實例用<tt>[[WordPress:Template_Tags/the_excerpt|the_excerpt()]]</tt>代替,如果你沒有建立你的文章的外部摘錄,這個函數(shù)將自動的顯示文章的前120個詞。 <pre> <div class="entry"> <?php the_excerpt(); ?> </div> </pre> ===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)。 <pre> <?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; ?> </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ù)讓定義標準的頁眉/邊欄/頁腳更加簡單。任何對這些文件的改動會立刻顯示給訪問者,你不用做任何工作。 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>。 To create your own <strong>different</strong> 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 <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> <?php include(TEMPLATEPATH . '/sideleft.php'); ?> </pre> 眼建立你自己的<strong>不同的</strong> 邊欄,你有兩種選擇。 # 把邊欄內(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>, 你可以象這樣把它涵蓋進去:: <pre> <?php include(TEMPLATEPATH . '/sideleft.php'); ?> </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ù)。如果這個你添加的元素是特別為某一模板使用的,最好是把它直接包含到那個模板中去。 ==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]]。 * [[WordPress:Templates|模板文件]] * [[WordPress:Template Tags|模板標簽]] * [[WordPress:Template Hierarchy|模板層次]] * [[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.themelab.com/2008/04/04/the-ultimate-guide-to-the-wordpress-loop/ WordPress Loop終極指南]
摘要:
請注意,您對站長百科的所有貢獻都可能被其他貢獻者編輯,修改或刪除。如果您不希望您的文字被任意修改和再散布,請不要提交。
您同時也要向我們保證您所提交的內(nèi)容是您自己所作,或得自一個不受版權(quán)保護或相似自由的來源(參閱
Wordpress-mediawiki:版權(quán)
的細節(jié))。
未經(jīng)許可,請勿提交受版權(quán)保護的作品!
取消
編輯幫助
(在新窗口中打開)
本頁使用的模板:
模板:WordPress導航
(
查看源代碼
)(受保護)
取自“
http://kktzf.com.cn/wiki/WordPress:The_Loop_in_Action
”