[WordPress] 外掛分享: Sort Query Posts

首頁外掛目錄 › Sort Query Posts
100+
安裝啟用
★★★★★
5/5 分(3 則評價)
4943 天前
最後更新
問題解決
WordPress 2.5+ v1.1 上架:2011-01-14

內容簡介

這是一個非常簡單且輕量的外掛(包括註釋共 73 行程式碼),可以實時對文章進行排序,而不需要向數據庫發送新的查詢請求(有助於提高性能)。

功能:

支持 query_posts 函数 的所有 order_by 及 order 值,除了需要數據庫查詢的 meta_value 及 meta_value_num。
支持對所有類型的文章進行排序,包括自定義文章類型及自定義文章類型 “存檔” 文章。

文檔:

此外掛將函數 sort_query_posts_by(string $order_by [, string $order]) 添加到全局上下文中。
第二個 $order 參數是可選的,其默認值為 asc(升序排列)。

在 循環遍歷 之前調用此函數可以更改文章排序方式。
調用此函數後,您可以像平常一樣顯示文章。

您可以按以下方式對文章進行排序:

作者
評論數
日期
ID
菜單順序
修改日期
父級
標題

此外掛在以下兩種情況下特別有用:

當您需要對 WordPress 從您提供的 URL 創建的查詢返回的文章進行重新排序時,例如,自定義文章類型的 “存檔” 文章就是這種情況的絕佳範例。
當您需要在同一頁面上多次顯示並按不同方式進行排序的自定義查詢所返回的文章時。

範例:

<?php sort_query_posts_by('title', 'desc'); ?>

上述範例將按標題降序排序文章,而不需要向數據庫發送新的查詢請求。
這樣排序是有利於性能的。

<?php sort_query_posts_by('ID'); ?>

上述範例將按文章 ID 升序排序。

<?php sort_query_posts_by('rand'); ?>

上述範例將隨機排序文章。當按隨機排序時,$order 參數將被忽略。

此外掛由 Túbal Martín 在 www.margenn.com 開發。

外掛標籤

開發者團隊

⬇ 下載最新版 (v1.1) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「Sort Query Posts」→ 直接安裝(推薦)
📦 歷史版本下載

原文外掛簡介

A really simple and lightweight plugin (73 lines of code – comments included) to sort posts on-the-fly without making a new query to the database (improves performance).
Features:

Supports all order_by and order values of the query_posts function except meta_value and meta_value_num (which require a database query).
Supports changing the order of all types of posts, including custom post type posts and custom post type “archive” posts.

Documentation
This plugin adds the function sort_query_posts_by(string $order_by [, string $order]) to the global context.
The second $order parameter is optional. Its default value is asc (ascending order).
Call this function before the loop to change how posts are ordered.
After calling this function you can show the posts as you normally would.
You can sort posts by:

author
comment_count
date
id
menu_order
modified
parent
title

This is specially useful in two cases:

When you need to reorder the posts returned by the query that WordPress creates from your given URL. Custom post type “archive” posts are a great example of this case.
When you need the posts returned by your customized query (e.g. query_posts()) to be shown more than once on the same page and ordered differently.

Examples:

The example above will sort posts by their title in descending order without making a new query to the database.
This way sorting is performance friendly.

The example above will sort posts by their ID in ascending order.

The example above will sort posts randomly. When sorting randomly $order is ignored.
Plugin developed by Túbal Martín at www.margenn.com.

延伸相關外掛

文章
Filter
Apply Filters
Mastodon