Display only top child of parent (WordPress)

Hey guys i am new to wordpress and was wondering how to Display only top child of parent. What i mean by that is, let’s say
Movies is the top category and the child of that is Disney, Pixar etc and the child of Disney is Action and Pixar is also Action
On movies category i only want to display Disney, and Pixar not Action
So lets put it like this:

Movies - Main
-Disney (Displayed in Movies Category)
--Action NOT DISPLAYED
-Pixar (Displayed in Movies Category)
--Action NOT DISPLAYED

but wordpress automatically displays it. how would I fix this?

Related posts

Leave a Reply

1 comment

  1. For this you’ll have first to get all top categories using get_categories and then get all categories that have each of the top categories as parent:

    $args = array(
            'orderby' => 'name',
            'parent' => 0
        );
    $top_categories = get_categories( $args );
    foreach ($top_categories as $top_category) {
        //here you display info about the top category or anything you want to do with it
        echo '<a href="' . get_category_link( $top_category->term_id ) . '">' . $top_category->name . '</a><br/>';
        $args = array(
                'orderby' => 'name',
                'parent' => $top_category->term_id
            );
        $child_categories = get_categories( $args );
        foreach ($child_categories as $child_category) {
        //here you write the code for child categories
            echo '<a href="' . get_category_link( $child_category->term_id ) . '">' . $child_category->name . '</a><br/>';
        }
    }