
內容簡介
這個外掛提供一個函數供開發人員使用,他們自己有一些程式碼可以根據特定條件獲取文章,並現在想要利用具有迴圈能力的模板標籤顯示這些文章。
WordPress 的模板標籤旨在在「迴圈」中使用。迴圈由一個 WP_Query 物件管理,該物件設置了各種全局變數和其自身的物件變數,供各種模板標籤使用。 WP_Query 物件的主要目的是實際查詢符合當前指定條件的文章。但是,如果您不需要查詢文章,因為您已經以其他方式獲取了它們,仍然可以通過此插件將這些文章注入到 WP_Query 中,以便利用模板標籤。
根據您要使用的模板標籤或您希望在迴圈中使用的邏輯,您可能需要手動配置一些查詢物件的變數。
示例:
<?php // 假設我們在側欄中
// 我們自己獲取了一些文章物件。
$posts = c2c_get_random_posts( 5, '' );
// 將文章注入
c2c_inject_query_posts( $posts );
// 現在讓我們透過模板標籤來顯示這些文章:
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="連結到 <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php endwhile;?>
<?php endif; ?>
連結: 外掛主頁 | 外掛目錄頁面 | GitHub | 作者主頁
開發人員文件
開發人員文件可在DEVELOPER-DOCS.md 中找到。該文件介紹了此外掛提供的模板標籤和鉤子。
概述來說,此外掛提供了以下模板標籤:
c2c_inject_query_posts() :模板標籤,可以像查詢物件一樣將一組文章注入到查詢物件中。
此外,此外掛提供了以下鉤子:
inject_query_posts_preserve_query_obj:覆蓋傳遞給函數的 $preserve_query_obj 參數的值。對於大多數用戶來說,這不是典型用法。
c2c_inject_query_posts :允許使用替代方法安全調用 c2c_inject_query_posts(),這樣如果外掛被停用或刪除,則調用該函數不會在您的網站上引起錯誤。
外掛標籤
開發者團隊
📦 歷史版本下載
原文外掛簡介
This plugin provides a function for use by developers who have their own code for fetching posts according to a given criteria and now want to make use of loop-aware template tags to display those posts.
WordPress’s template tags are intended to be used within ‘the loop’. The loop is managed by a WP_Query object which sets up various global variables and its own object variables for use by the various template tags. The primary purpose of a WP_Query object is to actually query the database for the posts that match the currently specified criteria. However, if you don’t need to query for posts since you already have them by some other means, you can still take advantage of the template tags by injecting those posts into the WP_Query via this plugin.
Depending on the template tags you are looking to use, or the logic you are hoping to employ within a loop, you may need to manually configure some of the query object’s variables.
Example:
Links: Plugin Homepage | Plugin Directory Page | GitHub | Author Homepage
Developer Documentation
Developer documentation can be found in DEVELOPER-DOCS.md. That documentation covers the template tag and hooks provided by the plugin.
As an overview, this is the template tag provided by the plugin:
c2c_inject_query_posts() : Template tag to inject an array of posts into a query object as if that query object had obtained those posts via a query.
These are the hooks provided by the plugin:
inject_query_posts_preserve_query_obj : Overrides the value of the $preserve_query_obj argument passed to the function. This is not typical usage for most users.
c2c_inject_query_posts : Allows use of an alternative approach to safely invoke c2c_inject_query_posts() in such a way that if the plugin were deactivated or deleted, then your calls to the function won’t cause errors in your site.
