0

通過登陸我們正在顯示類別。點擊任何類別顯示子類別。但是這也顯示了小孩的孩子。以下是我們的代碼來檢索子類別。帶分類標準兒童的自定義帖子類型

$terms = get_terms('msproduct'); 
if (! empty($terms) && ! is_wp_error($terms)){ 
    echo '<ul>'; 
    foreach ($terms as $term) { 
     echo '<li>' . $term->name . '</li>'; 
    } 
    echo '</ul>'; 
} 

請指教。我的分類結構如下

Commercial Ovan 
- 900 Series 
-- Product 1 
-- Product 2 
- 700 Series 
-- Product 1 
- 600 Series 
-- Product 1 
-- Product 2 
-- Product 3 
-- Product 4 

回答

0

您也許可以使用它。我不確定你的結構是什麼,但假設你在is_tax()上的頁面,然後檢查該條件可能工作。

$taxonomy = "msproduct"; 
$args = (is_tax()) ? array() : array('parent' => 0); // don't show children if you're on a taxonomy page 
$terms = get_terms($taxonomy, $args); 

if (! empty($terms) && ! is_wp_error($terms)){ 
    echo '<ul>'; 
    foreach ($terms as $term) { 
     echo '<li>' . $term->name . '</li>'; 
    } 
    echo '</ul>'; 
} 

參考:

https://developer.wordpress.org/reference/functions/get_terms/ https://codex.wordpress.org/Function_Reference/is_tax

+0

謝謝你的回覆。商業Ovan是包含直接產品和子類別的主要類別。如果涉及到類別,它可以有一個產品或多個產品。 index.php,taxonomy-msproduct.php,single-msproduct.php是頁面。如果點擊它的直接產品將帶我到單頁面的所有細節。但如果它包含多於一種產品,那麼會顯示一個產品循環。這是關於 –

相關問題