简体   繁体   English

WordPress自定义帖子->显示当前父母的孩子类别

[英]Wordpress Custom Post -> Show Children Category of current Parent

I have this code: 我有以下代码:

<?php

$parent_cat_arg = array('hide_empty' => false, 'parent' => 0 );
$parent_cat = get_terms('category',$parent_cat_arg);//category name

foreach ($parent_cat as $catVal) {

echo '<h2>'.$catVal->name.'</h2>'; //Parent Category

$child_arg = array( 'hide_empty' => false, 'parent' => $catVal->term_id );
$child_cat = get_terms( 'category', $child_arg );

echo '<ul>';
    foreach( $child_cat as $child_term ) {
        $term_link = get_term_link( $child_term );
        echo '<li><a href=" ' . esc_url( $term_link ) . ' ">' .$child_term->name . '</a></li>'; //Child Category
    }
echo '</ul>';

}
?>

Which I use on the single-products.php template. 我在single-products.php模板上使用了该模板。 This works fine, except it outputs all categories and all sub categories of my custom post type. 除输出我自定义帖子类型的所有类别和所有子类别外,此方法工作正常。

How do I get it to only show the sub categories of the current parent category? 我如何只显示当前父类别的子类别?

Did you tried to use the 'child_of'instead of 'parent' in your array? 您是否尝试在数组中使用'child_of'而不是'parent'?

$parent_cat_arg = array('hide_empty' => false, 'child_of' => 0 ); $ parent_cat_arg = array('hide_empty'=>否,'child_of'=> 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM