2017-04-10 102 views
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是页面。如果点击它的直接产品将带我到单页面的所有细节。但如果它包含多于一种产品,那么会显示一个产品循环。这是关于 –

相关问题