S
S
Sergey Shilov2018-10-10 12:58:52
WordPress
Sergey Shilov, 2018-10-10 12:58:52

How to display child categories on the category page?

It is necessary to display all child categories relative to the current parent.
I use this design, but it doesn't work:

<?php
                $categories = get_the_category(); 
                $parent_id = $categories[0]->cat_ID;

                # получаем дочерние рубрики
                $sub_cats = get_categories( array(
                    'child_of' => $parent_id,
                    'hide_empty' => 0
                ) );
                if( $sub_cats ){
                    foreach( $sub_cats as $cat ){

                        // Данные в объекте $cat

                        // $cat->term_id
                        // $cat->name (Рубрика 1)
                        // $cat->slug (rubrika-1)
                        // $cat->term_group (0)
                        // $cat->term_taxonomy_id (4)
                        // $cat->taxonomy (category)
                        // $cat->description ()
                        // $cat->parent (0)
                        // $cat->count (14)
                        // $cat->object_id (2743)
                        // $cat->cat_ID (4)
                        // $cat->category_count (14)
                        // $cat->category_description ()
                        // $cat->cat_name (Рубрика 1)
                        // $cat->category_nicename (rubrika-1)
                        // $cat->category_parent (0)

                        echo '<h3>'. $cat->name .'</h3>';

                    }

                    wp_reset_postdata(); // сбрасываем глобальную переменную пост
                } ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Pupkin, 2018-10-11
@HectorPrima

Your code is working. Maybe you are inserting into the wrong template?
Template Hierarchy

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question