問題描述

我有帖子型別’product’ 和分層分類’types’ 附加到它。在這個分類中,我有這樣的術語:’dry-clean’,’washer’ 等。

在我的情況下,我需要顯示,例如:

  1. http://example.com/types/washer – > 顯示所有子條款

  2. http://example.com/types/washer/ {subterm} – > 顯示所有帖子

我的問題是:

  1. 如何獲得父條款的永久連結 URL?我嘗試上面的 URL,但是導致 404 。

  2. 模板檔案如何適用於這種問題?

    1. taxonomy-types-washer.php 是否適合#1 的情況?還是應該建立 taxonomy-types.php 並在那裡建立邏輯?

    2. 在我看來,在掃描 template hierarchy 之後,我可能需要 taxonomy-types-{term_parent}.php 列出所有子條款和 taxonomy-types.php 以子條款列出所有產品。

    3. 最終要顯示每個產品,我將不得不建立 single-product.php

  3. 在一個稍微無關的問題。我發現當我沒有任何帖子時,archive-{posttype}.php 不起作用。有沒有解決方案? (我應該創造不同的問題,還是堅持這個)?

UPDATE

我檢查了我的 rewrite_rules 選項,它並沒有列出 [types] 。我不知道為什麼為了測試,我把我的屁股變成 product-types,沖洗固定連結,並列出這一點:

[product-types/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?types=$matches[1]&feed=$matches[2]
[product-types/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?types=$matches[1]&feed=$matches[2]
[product-types/([^/]+)/page/?([0-9]{1,})/?$] => index.php?types=$matches[1]&paged=$matches[2]
[product-types/([^/]+)/?$] => index.php?types=$matches[1]

所以我想現在註冊了。我嘗試載入 URL product-types/washer 它到 404. 我嘗試載入 URL index.php?types=washer 。它也是 404. 現在,我有這個檔案:

  • taxonomy-types-washer.php

  • taxonomy-types.php

所以,我不知道這個有什麼問題:(

更新#2

我發現問題。這是因為我錯過了'rewrite'=>array('hierarchical'=>true)

這是新的 rewrite_rules

[product-types/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?types=$matches[1]&feed=$matches[2]
[product-types/(.+?)/(feed|rdf|rss|rss2|atom)/?$] => index.php?types=$matches[1]&feed=$matches[2]
[product-types/(.+?)/page/?([0-9]{1,})/?$] => index.php?types=$matches[1]&paged=$matches[2]
[product-types/(.+?)/?$] => index.php?types=$matches[1]

最佳解決方法

WP 3.1 之後支援這些型別的 URL:

register_taxonomy( 'types', 'post', array(
  'hierarchical' => true,
  'rewrite' => array( 'hierarchical' => true ),
  ...
);

記住在進行更改後重新整理重寫規則。

您將用於父項和子項的模板是 taxonomy-types.php:

$current_term = get_queried_object();

if ( 0 == $current_term->parent ) {
  // it's a parent term; display it's child terms using wp_list_categories() etc.
} else {
  // it's a child term; display the posts using The Loop etc.
}

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。