Display post from child category

I got a code here where it post all my custom categories then if you click a category if the category has child category it will display all the child and if there is no child category it will display all the post from the child category selected. My problem I can’t seem to list all the posts of the child category.

<form method=”get”>
<?php
$parent_term_id = $_GET[“get_cat”]; // term id of parent term

//$termchildren = get_terms(‘category’,array(‘child_of’ => $parent_id));
$taxonomies = array(
‘taxonomy’ => ‘category_download’
);

$args = array(
‘parent’ => $parent_term_id,
// ‘child_of’ => $parent_term_id,
‘hide_empty’ => FALSE,
);

$terms = get_terms($taxonomies, $args);
if (sizeof($terms)>0) {
echo ‘ <div class=”categories”> ‘;
echo ‘<p> Sub Categories of ‘. get_cat_name( $parent_term_id ) .'</p>’;

foreach ( $terms as $term ) { ?>
<button name=”get_cat” value=”<?php echo $term->term_id; ?>”><?php echo $term->name; ?></button><br>

<?php }
echo ‘</div><!– categories div end–>’;
} else {

echo ‘ <div class=”categories”> ‘;
echo ‘<p> Sub Categories of ‘. get_cat_name( $parent_term_id ) .'</p>’;

foreach ( $terms as $term2 ) { ?>
<button name=”get_cat” value=”<?php echo $term2->term_id; ?>”><?php echo $term2->name; ?></button><br>
<?php
$args_post=array(
‘post_type’ => ‘download’,
‘post_status’ => ‘publish’,
‘posts_per_page’ => 45,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘category_download’,
‘field’ => ‘term_id’,
‘value’ => $term2->term_id
)
));
?>
<?php
$download_query = new WP_Query($args_post);?>
<?php while ($download_query->have_posts()) : $download_query->the_post(); ?>
<?php echo the_title(); ?><br>
<?php endwhile; wp_reset_query(); ?>

<?php }
echo ‘</div><!– categories div end–>’;

?> <strong> <?php echo $parent_term_id; ?> </strong><br>
<?php } ?>
</form>

Read more here:: Display post from child category

Leave a Reply

Your email address will not be published. Required fields are marked *