简体   繁体   中英

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. 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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