[WordPress] 外掛分享: Custom Taxonomy Sort

首頁外掛目錄 › Custom Taxonomy Sort
400+
安裝啟用
★★★★
4/5 分(14 則評價)
5209 天前
最後更新
問題解決
WordPress 3.1+ v1.1.5 上架:2011-07-03

內容簡介

令人驚訝的是,WordPress 不提供自定義排序分類法的機制。分類法只能以名稱或 id 排序,無法自定義。Custom Taxonomy Sort 讓普通用戶有機會定義並顯示特定順序的項目。安裝 Custom Taxonomy Sort 後,每個分類法項目將有與其關聯的「tax-order」值。執行以下操作指定排序:

前往任何分類法添加或編輯畫面(例如,wp-admin/edit-tags.php?taxonomy=category)
填寫「序號」欄位的數值。預設為升序排序(即從小到大排序,例如 1、2、3)。這些值可以在新增分類法畫面、編輯分類法畫面或透過「快速編輯」面板中新增。
觀察所有項目根據指定的順序自動排序

Custom Taxonomy Sort 會自動將排序應用於所有顯示該分類法項目的實例。您只需定義該順序即可。

手動模式

除了自動排序項目之外,Custom Taxonomy Sort 還可以允許開發人員覆蓋自動排序,以更細緻地控制在 WordPress 不同部分顯示項目的方式。手動模式可以通過在 Custom Taxonomy Sort 設置頁面(wp-admin/options-general.php?page=custom-taxonomy-sort-settings)中將「自動排序」更改為「關閉」來啟動。切換「自動排序」為「關閉」後,項目將不再按照自定義順序自動排序。取而代之的是,可以在「orderby」引數的新參數中調用自定義排序順序。Custom Taxonomy Sort 允許您使用以下引數使用「get_terms」來訪問自定義排序項目清單

<?php get_terms('orderby=custom_sort'); ?>

現在,您的項目只會按照您指定的順序在想要按此順序排序的地方排序。顯示項目的所有其他實例將恢復 WordPress 默認的按名稱排序。此外,您可以通過添加「order」引數特別將項目按照升序('ASC'; 預設,例如 1、2、3)或降序('DESC';例如 3、2、1)排序。

<?php get_terms('orderby=custom_sort&order=ASC'); ?>

<?php get_terms('orderby=custom_sort&order=DESC'); ?>

鳴謝

感謝 Jacob M Goldman 的優秀 Simple Term Meta 外掛,使得程式編寫期間的項目元素非常簡單。
感謝 hydrowire 指出 1.1.1 中已修復的錯誤
感謝“Mosey”指出了不適用於自定義分類法的順序排序錯誤
感謝 DUCK__BOY1981 指出了欄位值被不適當覆蓋的錯誤
感謝 @martindj 指出了欄位值不正確返回的錯誤

外掛標籤

開發者團隊

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

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

原文外掛簡介

Surprisingly, WordPress does not provide a mechanism for sorting taxonomies by a custom defined order. Taxonomies can only be sorted by name or id. Custom Taxonomy Sort allows the average user a mechanism to define and display terms in a specified order. After installing Custom Taxonomy Sort, each taxonomy term will have the ability to have a “tax-order” value associated with it. This order is specified by doing the following:

Go to any taxonomy add or edit screen (e.g., wp-admin/edit-tags.php?taxonomy=category)
Fill in a numeric value for the “Order” field. By default, the order will be ascending, meaning it will sort from low to high (e.g., 1, 2, 3). These values can be added on the Add Taxonomy screen, the Edit Taxonomy screen, or through the Quick Edit panel.
Observe all of your terms automagically being sorted in the order you specified

Custom Taxonomy Sort automatically applies the sort order to all instances in which the terms are displayed. All you need to do is define that order.
Manual Mode
In addition to automatically sorting the terms, Custom Taxonomy Sort allows developers to override the automatic sort to offer finer control over how terms are displayed in different parts of WordPress. Manual mode can be started by changing “Automatic Sort” to “Off” in the Custom Taxonomy Sort Settings page (wp-admin/options-general.php?page=custom-taxonomy-sort-settings). Once “Automatic Sort” is switched to “Off”, the terms will no longer be sorted automatically by the custom order. Instead, the custom sort order can be envoked with a new parameter for the “orderby” argument. Custom Taxonomy Sort allows you to use the following argument to access a custom sorted list of terms using “get_terms”

Voila! Now, your terms will only be sorted by the specified order in the places that you want it sorted in this order. All other instances of displaying terms will revert to WordPress’ default of sorting the terms by name. Additionally, you can specifically have the terms sort in ascending (‘ASC’; default; e.g., 1, 2, 3) or descending (‘DESC’; e.g., 3, 2, 1) order by adding the “order” argument.

Acknowledgements

Thanks to Jacob M Goldman for his excellent Simple Term Meta plugin, which made programming the term meta quite simple.
Thanks to hydrowire for pointing out a bug fixed in 1.1.1
Thanks to “Mosey” for pointing out a bug with a situation in which order did not work for a custom taxonomy
Thanks to DUCK__BOY1981 for pointing out a bug where the column values where overridden inappropriately
Thanks to @martindj for pointing out a bug where the column value returned incorrectly

延伸相關外掛

文章
Filter
Mastodon