循環是WordPress用于通過主題的模板文件輸出帖子的默認機制。 檢索的帖子數量取決于閱讀設置中定義的每頁顯示的帖子數量。 在循環中,WordPress將檢索要顯示在當前頁面上的每個帖子,并根據您的主題說明進行格式化。
循環從WordPress數據庫中提取每個帖子的數據,并插入適當的信息代替每個模板標簽。 將為每個帖子處理循環中的任何HTML或PHP代碼。
簡單來說,循環是它的名字:它循環遍歷當前頁面檢索到的每個帖子一次,并執行您的主題中指定的操作。
您可以使用循環來執行多種不同的操作,例如:
- 在您的博客主頁上顯示帖子標題和摘錄;
- 在單個帖子上顯示內容和評論;
- 使用模板標簽在單個頁面上顯示內容; 和
- 顯示來自Custom Post Types和Custom Fields的數據。
- 您可以在模板文件中自定義循環,以顯示和操作不同的內容。
## 循環詳情
基本循環是:
```
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
... Display post content
<?php endwhile; ?>
<?php endif; ?>
```
這個循環說,當有帖子時,循環并顯示帖子。 詳細介紹:
have_posts()函數檢查是否有任何帖子。
如果有帖子,只要括號中的條件在邏輯上為真,則while循環將繼續執行。 只要have_posts()繼續為true,循環將繼續。
## 使用循環
循環應放在index.php中,以及用于顯示帖子信息的任何其他模板中。 因為你不想一遍又一遍地復制你的頭,循環應該總是在調用get_header()之后放置。 例如:
```
<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
... Display post content
<?php endwhile; endif; ?>
```
在上面的例子中,循環的結尾顯示為一個結尾和endif。 循環必須始終以相同的if和while語句開始,如上所述,并且必須以相同的結束語句結尾。
您要應用于所有帖子的任何模板標簽必須存在于開始和結束語句之間。
>[warning] 提示:如果沒有符合指定條件的帖子可用,您可以添加自定義的404“未找到”消息。 消息必須放在endwhile和endif語句之間,如下面的示例所示。
一個非常簡單的index.php文件將如下所示:
```
<?php
get_header();
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile;
else :
_e( 'Sorry, no posts matched your criteria.', 'textdomain' );
endif;
get_sidebar();
get_footer();
?>
```
## 循環可以顯示
循環可以為每個帖子顯示多個不同的元素。 例如,許多主題中使用的常見模板標簽是:
- next_post_link() – 在這篇文章后按時間順序發布的一篇鏈接
- previous_post_link() – 在這篇文章之前根據時間順序發布了一篇鏈接
- the_category() – 與正在查看的帖子或頁面相關聯的類別或類別
- the_author() – 作者的帖子或頁面
- the_content() – 頁面的主要內容
- the_excerpt() – 一個帖子的主要內容的前55個字,后跟一個省略號(...)或閱讀更多鏈接到完整的帖子。 您還可以使用帖子的“摘錄”字段來自定義特定摘錄的長度。
- the_ID() – 帖子或頁面的ID
- the_meta() – 與帖子或頁面關聯的自定義字段
- the_shortlink() – 使用網站的網址和帖子或頁面的ID鏈接到頁面或帖子
- the_tags() –與帖子相關聯的標簽或標簽
- the_title() – 帖子或頁面的標題
- the_time() – 帖子或頁面的時間或日期。 這可以使用標準php日期功能格式化來定制。
您還可以使用條件標簽,例如:
- is_home() – 如果當前頁面是主頁,則返回true
- is_admin() – 如果是管理員,返回true,否則返回false
- is_single() – 如果頁面當前顯示單個帖子,則返回true
- is_page() – 如果頁面當前顯示單個頁面,則返回true
- is_page_template() – 可用于確定頁面是否正在使用特定的模板,例如:is_page_template('about-page.php')
- is_category() – 如果頁面或帖子具有指定的類別,則返回true,例如:is_category('news')
- is_tag() – 如果頁面或帖子具有指定的標簽,則返回true
- is_author() – 如果在作者的歸檔頁面內返回true
- is_search() – 如果當前頁面是搜索結果頁面,則返回true
- is_404() – 如果當前頁不存在,則返回true
- has_excerpt() – 如果帖子或頁面有摘錄,則返回true
# 示例
讓我們來看一下循環中的一些例子:
## 基本例子
### 博客歸檔
大多數博客都有一個博客歸檔頁面,可以顯示一些內容,包括帖子標題,縮略圖和摘錄。 下面的示例顯示了一個簡單的循環,檢查是否有任何帖子,如果有的話,輸出每個帖子的標題,縮略圖和摘錄。 如果沒有帖子,它會以括號顯示消息。
```
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_post_thumbnail(); ?>
<?php the_excerpt(); ?>
<?php endwhile; else: ?>
<?php _e( 'Sorry, no posts matched your criteria.', 'textdomain' ); ?>
<?php endif; ?>
```
## 個人帖子
在WordPress中,每個帖子都有自己的頁面,顯示該帖子的相關信息。 模板標簽允許您自定義要顯示的信息。
在下面的例子中,循環輸出帖子的標題和內容。 您可以在帖子或頁面模板文件中使用此示例來顯示有關該帖子的最基本信息。 您還可以自定義此模板以向帖子添加更多數據,例如類別。
```
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; else: ?>
<?php _e( 'Sorry, no pages matched your criteria.', 'textdomain' ); ?>
<?php endif; ?>
```
# 中間例子
## 不同類別的風格帖子
下面的例子有幾件事情:
首先,它顯示每個帖子的標題,時間,作者,內容和類別,類似于上面的單個帖子示例。
接下來,通過使用in_category()模板標簽,可以使類別ID為“3”的帖子的樣式不同。
此示例中的代碼注釋在循環的每個階段提供了詳細信息:
```
// Start the Loop.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
/* * See if the current post is in category 3.
* If it is, the div is given the CSS class "post-category-three".
* Otherwise, the div is given the CSS class "post".
*/
if ( in_category( 3 ) ) : ?>
<div class="post-category-three">
<?php else : ?>
<div class="post">
<?php endif; ?>
// Display the post's title.
<h2><?php the_title() ?></h2>
// Display a link to other posts by this posts author.
<small><?php _e( 'Posted by ', 'textdomain' ); the_author_posts_link() ?></small>
// Display the post's content in a div.
<div class="entry">
<?php the_content() ?>
</div>
// Display a comma separated list of the post's categories.
<?php _e( 'Posted in ', 'textdomain' ); the_category( ', ' ); ?>
// closes the first div box with the class of "post" or "post-cat-three"
</div>
// Stop the Loop, but allow for a "if not posts" situation
<?php endwhile; else :
/*
* The very first "if" tested to see if there were any posts to
* display. This "else" part tells what do if there weren't any.
*/
_e( 'Sorry, no posts matched your criteria.', 'textdomain' );
// Completely stop the Loop.
endif;
?>
```
# 刪除此部分
>[warning] 注意:此部分需要刪除,但存在是因為手冊插件中的錯誤無法正確顯示主題框中的下一部分。
## 多重循環
在某些情況下,您可能需要使用多個循環。 例如,您可能希望在頁面頂部的內容列表表中顯示帖子的標題,然后在頁面上進一步顯示內容。 由于查詢沒有被改變,所以我們需要在第二次循環訪問這些信息時,回滾循環。 為此,我們將使用函數rewind_posts()。
### 使用rewind_posts()
您可以再次使用rewind_posts()循環遍歷相同的查詢。 如果要在頁面上的不同位置顯示相同的查詢兩次,這將非常有用。
以下是正在使用的rewind_posts()的示例:
```
// Start the main loop
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_title();
endwhile;
endif;
// Use rewind_posts() to use the query a second time.
rewind_posts();
// Start a new loop
while ( have_posts() ) : the_post();
the_content();
endwhile;
?>
```
## 創建輔助查詢和循環
使用具有相同查詢的兩個循環相對容易,但并不總是您需要的。 相反,您通常會創建一個輔助查詢來在模板上顯示不同的內容。 例如,您可能希望在同一頁面上顯示兩組帖子,但對每個組執行不同的操作。 如下所示,一個常見的例子是顯示單個帖子,其中包含單個帖子下方相同類別的帖子列表。
```
<?php
// The main query.
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_title();
the_content();
endwhile;
else :
// When no posts are found, output this text.
_e( 'Sorry, no posts matched your criteria.' );
endif;
wp_reset_postdata();
/*
* The secondary query. Note that you can use any category name here. In our example,
* we use "example-category".
*/
$secondary_query = new WP_Query( 'category_name=example-category' );
// The second loop. if ( $secondary_query->have_posts() )
echo '<ul>';
while ( $secondary_query->have_posts() ) :
$secondary_query->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
echo '</ul>';
endif;
wp_reset_postdata();
?>
```
如上例所示,我們首先顯示一個常規循環。 然后我們定義一個使用WP_Query查詢特定類別的新變量; 在我們的例子中,我們選擇了示例類別的插件。
>[warning] 請注意,上述示例中的常規循環有一個區別:它調用wp_reset_postdata()來重置帖子數據。 在您可以使用第二個循環之前,您需要重置帖子數據。 有兩種方法可以做到這一點:
- 通過使用rewind_posts()函數; 要么
- 通過創建新的查詢對象。
## 重置多個循環
在您重置它們的模板中使用多個循環時,這很重要。 不這樣做可能會導致意外的結果,因為數據在全局$post變量中的存儲和使用。 有三種主要的方法來重置循環,具體取決于它們的調用方式。
- wp_reset_postdata()
- wp_reset_query()
- rewind_posts()
### 使用wp_reset_postdata()
當您使用WP_Query運行自定義或多個循環時,請使用wp_reset_postdata()。 此函數將全局$post變量恢復到主查詢中的當前帖子。 如果您遵循最佳做法,這是您將用來重置循環的最常用功能。
要正確使用此功能,請在使用WP_Query的任何循環后放置以下代碼:
```
<?php wp_reset_postdata(); ?>
```
這是一個使用WP_Query的循環的示例,它使用wp_reset_postdata()重置。
```
<?php
// Example argument that defines three posts per page.
$args = array( 'posts_per_page' => 3 );
// Variable to call WP_Query.
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
// Start the Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
the_excerpt();
// End the Loop
endwhile;
else:
// If no posts match this query, output this text.
_e( 'Sorry, no posts matched your criteria.', 'textdomain' );
endif;
wp_reset_postdata();
?>
```
## 使用wp_reset_query()
使用wp_reset_query()將WP_Query和全局$post數據恢復到原始主查詢。 如果在循環中使用query_posts(),則必須使用此功能來重置循環。 您可以在使用WP_Query自定義循環后使用它,因為它在運行時實際調用wp_reset_postdata()。 然而,最好的做法是使用wp_reset_postdata()與涉及WP_Query的任何自定義循環。
>[warning] 請注意:query_posts()不是最佳實踐,如果可能的話應該避免。 因此,您不應該對wp_reset_query()有很多用處。
要正確使用此功能,請在使用query_posts()的任何循環后面放置以下代碼。
```
<?php wp_reset_query(); ?>
```
- 簡介
- 主題開發
- WordPress許可證
- 什么是主題
- 開發環境
- 主題開發示例
- 主題基礎
- 模板文件
- 主樣式表(style.css)
- 文章類型
- 規劃主題文件
- 模板層級
- 模板標簽
- 循環
- 主題函數
- 連接主題文件和目錄
- 使用CSS和JavaScript
- 條件標簽
- 類別,標簽和自定義分類
- 模板文件
- 內容模板文件
- 頁面模板文件
- 附件模板文件
- 自定義內容類型
- 部分和其他模板文件
- 評論模板
- 分類模板
- 404頁面
- 主題功能
- 核心支持的功能
- 管理菜單
- 自定義Headers
- 自定義Logo
- 文章格式
- 置頂文章
- Sidebars
- Widgets
- 導航菜單
- 分頁
- 媒體
- Audio
- Images
- Galleries
- Video
- 精選圖片和縮略圖
- 國際化
- 本地化
- 輔助功能
- 主題選項 – 自定義API
- 定制對象
- 改進用戶體驗的工具
- 定制JavaScript API
- JavaScript / Underscore.js渲染的自定義控件
- 高級用法
- 主題安全
- 數據消毒/逃避
- 數據驗證
- 使用隨機數
- 常見漏洞
- 高級主題
- 子主題
- UI最佳實踐
- JavaScript最佳做法
- 主題單元測試
- 驗證你的主題
- Plugin API Hooks
- 發布你的主題
- 所需的主題文件
- 測試
- 主題評論指南
- 寫文檔
- 提交你的主題到WordPress.org
- 參考文獻
- 模板標簽列表
- 條件標簽列表
- 編碼標準
- HTML編碼標準
- CSS編碼標準
- JavaScript編碼標準
- PHP編碼標準
- 插件開發
- 插件開發簡介
- 什么是插件
- 插件基礎
- 頭部要求
- 包括軟件許可證
- 啟用 / 停用 Hooks
- 卸載方法
- 最佳做法
- 插件安全
- 檢查用戶功能
- 數據驗證
- 保護輸入
- 保護輸出
- 隨機數
- Hooks
- Actions
- Filters
- 自定義Hooks
- 高級主題
- 管理菜單
- 頂級菜單
- 子菜單
- 短代碼
- 基本短碼
- 封閉短碼
- 帶參數的短代碼
- TinyMCE增強型短碼
- 設置
- 設置API
- 使用設置API
- 選項API
- 自定義設置頁面
- 元數據
- 管理帖子元數據
- 自定義元數據
- 渲染元數據
- 自定義文章類型
- 注冊自定義文章類型
- 使用自定義文章類型
- 分類
- 使用自定義分類
- 在WP 4.2+中使用“split術語”
- 用戶
- 創建和管理用戶
- 使用用戶元數據
- 角色和功能
- HTTP API
- JavaScript
- jQuery
- Ajax
- 服務器端PHP和入隊
- Heartbeat API
- 概要
- 計劃任務
- 了解WP-Cron計劃
- 安排WP-Cron 事件
- 將WP-Cron掛接到系統任務計劃程序中
- WP-Cron簡單測試
- 國際化
- 本地化
- 如何國際化您的插件
- 國際化安全
- WordPress.org
- 詳細插件指南
- 規劃您的插件
- 如何使用Subversion
- 插件開發者常見問題
- 開發工具
- Debug Bar 和附加組件
- 輔助插件
- REST API手冊
- 資源
- 文章
- 文章修訂
- 文章類型
- 文章狀態
- 類別
- 標簽
- 頁面
- 評論
- 分類
- 媒體
- 用戶
- 設置
- 使用REST API
- 全局參數
- 分頁
- 鏈接和嵌入
- 發現
- 認證
- 經常問的問題
- 骨干JavaScript客戶端
- 客戶端庫
- 擴展REST API
- 添加自定義端點
- 自定義內容類型
- 修改回應
- 模式
- 詞匯表
- 路由和端點
- 控制器類