[WordPress] 外掛分享: Single Category Permalink

首頁外掛目錄 › Single Category Permalink
WordPress 外掛 Single Category Permalink 的封面圖片
500+
安裝啟用
★★★★★
5/5 分(5 則評價)
1630 天前
最後更新
問題解決
WordPress 4.6+ v2.5.2 上架:2011-07-17

內容簡介

這款外掛可以將連結 (分類或文章)的完整分類架構縮減為僅顯示最低階層的分類。

預設情況下,WordPress 會將自訂連結結構中的 %category% 佔位符號,替換為該文章的第一個符合的分類的完整分類架構。例如,如果網站的分類架構如下:

應用程式
|_ 桌面
|_ Web
|_ WordPress

若你的自訂連結結構設定為 %category%/%year%/%monthnum%/%day%/%postname%,而文章標題為「最佳外掛」且歸屬於「WordPress」分類,則文章的連結將會是:

https://www.example.com/applications/web/wordpress/2008/01/15/best-plugins

如果你啟用了 Single Category Permalink 外掛,則文章的連結 (且會被網誌所辨識) 將會是:

https://www.example.com/wordpress/2008/01/15/best-plugins

要讓分類成為文章連結結構必要的一部分,必須在「設定」-「永久連結」管理頁面中明確地定義 %category% 為自訂結構的一部分,例如: /%category%/%postname%。

對於分類連結,%category% 是被默認隱含為「分類基底」值 (或者是預設的分類基底如果沒有指定的話)。因此,如果你的分類基底是「category」,上述例子的分類列表頁面將會列出位於「WordPress」分類內的文章:

https://www.example.com/category/applications/web/wordpress/

如果啟用了這個外掛,作為連結的網址會變成:

https://www.example.com/category/wordpress/

注意: 完整分類和文章的連結將會繼續運作。這個外掛會對瀏覽器和搜尋引擎發出 302 導向重新導向至短網址。

相關頁面: 外掛首頁 | 外掛目錄頁 | GitHub | 作者網站

Hook

這個外掛提供一個適用的 Hook。使用這個 Hook 的程式碼最好放在 mu-plugin 或網站特定的外掛中 (超出本說明文件的範圍)。也可以將它們放在你的目前主題的 functions.php 檔案中。

c2c_single_category_redirect_status (filter)

這個 Hook 可以讓你指定重新導向所使用的 HTTP 狀態碼。預設情況下是 302。

參數:

$status (integer) : 預設的 HTTP 狀態碼

範例:

/**
* 將重新導向的 HTTP 狀態碼設為 302。
*
* @param int $code HTTP 狀態碼。預設是 301。
* @return int
*/
function scp_change_redirect_status( $code ) {
return 302;
}
add_filter( 'c2c_single_category_redirect_status', 'scp_change_redirect_status' );

外掛標籤

開發者團隊

⬇ 下載最新版 (v2.5.2) 或搜尋安裝

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

原文外掛簡介

Reduce permalinks (category or post) that include entire hierarchy of categories to just having the lowest category in the hierarchy.
By default, WordPress replaces the %category% permalink tag in a custom permalink structure with the entire hierarchy of categories for the post’s first matching category. For example, assuming your site has a hierarchical category structure like so:
Applications
|_ Desktop
|_ Web
|_ WordPress

By default, if you have a permalink structure defined as %category%/%year%/%monthnum%/%day%/%postname%, your post titled “Best Plugins” assigned to the “WordPress” category would have a permalink of:
https://www.example.com/applications/web/wordpress/2008/01/15/best-plugins

If you activate the Single Category Permalink plugin, this would be the permalink generated for the post (and recognized by the blog):
https://www.example.com/wordpress/2008/01/15/best-plugins

In order for a category to be used as part of a post’s permalink structure, %category% must be explicitly defined in the Settings -> Permalinks admin page as part of a custom structure, i.e. /%category%/%postname%.
For category links, %category% is implied to follow the value set as the “Category base” (or the default category base if none is specified). So if your category base is ‘category’, the above example would list posts in the ‘WordPress’ category on this category listing page:
https://www.example.com/category/applications/web/wordpress/

With this plugin activated, that link would become:
https://www.example.com/category/wordpress/

NOTE: The fully hierarchical category and post permalinks will continue to work. The plugin issues a 302 redirect to browsers and search engines pointing them to the shorter URL.
Links: Plugin Homepage | Plugin Directory Page | GitHub | Author Homepage
Hooks
The plugin exposes one filter for hooking. Code using this filter should ideally be put into a mu-plugin or site-specific plugin (which is beyond the scope of this readme to explain). Less ideally, you could put them in your active theme’s functions.php file.
c2c_single_category_redirect_status (filter)
The ‘c2c_single_category_redirect_status’ hook allows you to specify an HTTP status code used for the redirect. By default this is 302.
Arguments:

$status (integer) : The default HTTP status code

Example:
/**
* Change the redirection HTTP status to a 302.
*
* @param int $code The HTTP status code. By default 301.
* @return int
*/
function scp_change_redirect_status( $code ) {
return 302;
}
add_filter( 'c2c_single_category_redirect_status', 'scp_change_redirect_status' );

延伸相關外掛

文章
Filter
Apply Filters
Mastodon