Search code examples
wordpress-themingmenuitem

How to display sub-categories of a category page?


I have this code to display a menu in the header, but I want to show the child categories for the selected category page not the parents categories.

  <?php
               $categories = get_the_category();
                $category_id = $categories[0]->cat_ID;
                foreach ( $category_id as $navItem ) {
                    $class_names = $value = '';

                    $classes = empty( $navItem->classes ) ? array() : (array) $navItem->classes;
                    $classes[] = 'menu-item-' . $navItem->ID;

                    $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $navItem ) );

                    if ( in_array( 'current-menu-item', $classes ) )
                        $class_names .= ' active';

                    $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

                    $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $navItem->ID, $navItem );
                    $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
                    echo '<li ' . $id . ' ' . $class_names . '><a href="'.$navItem->url.'" title="'.$navItem->title.'">'.$navItem->title.'</a></li>';
                }
                ?>

thank you


Solution

  • Ok I have made some search and I was able to fix this one

    this is the code

       <?php
             if(is_category() && the_category_ID(false) != NULL)
            {
            $categories = get_categories(array("child_of" => the_category_ID(false), "hide_empty" => 0));
    
    
            foreach ($categories as $category)
                    {
    
                        $class_names = $value = '';
    
                        $classes = (array) $navitem ='';
                        $classes[] = 'menu-item-' . $category->cat_ID;
    
                        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $category ) );
    
                        if ( in_array( 'current-menu-item', $classes ) )
                            $class_names .= ' active';
    
                        $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
    
                        $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $category->cat_ID, $category );
                        $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
    
                          echo '<li ' . $id . ' ' . $class_names . '><a href="'.get_bloginfo("url") .'/category/' .$category->slug.'" title="'.$category->cat_name.'">'.$category->cat_name.'</a></li>'; 
                          } 
    
    
    
                    }
                    ?>
    

    hope it helps someone too

    the_category_ID()
    

    this one gets the ID of the current page category and we use false so it wont print the ID

    and then loop inside for the sub-categories or child categories and gets their names + slug for URL