1

I wish to display only first level of subcategories on current category page (if any).

I tried this code:

<?php
        $args=array(
            'child_of' => $cat-id,
            'hide_empty' => 0,
            'orderby' => 'name',
            'order' => 'ASC',
            'depth' => '1' //NOT WORKING

        );
        $categories=get_categories($args);
        foreach($categories as $category) {
            echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';  }
        ?>

But, it shows all children, and children's children (grandchildren). I tried with "'depth' => '1'" but it is ignored.

How to show only first level of children (if exist) on category page?

3

You are using get_categories() function and this function have not 'depth' argument. Also php is not supporting $cat-id variable. it would be $cat_id.

So try this kind of code:

$cat_id = get_query_var('cat');
$args=array(
            'parent' => $cat_id,
            'hide_empty' => 0,
            'orderby' => 'name',
            'order' => 'ASC'
        );
  • I tried that, but now it give me just first level categories on any category/subcategory. – Advanced SEO Aug 6 '14 at 7:02
  • 1
    I managed to do this with adding '$cat_id = get_query_var('cat');'. It works now. – Advanced SEO Aug 6 '14 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.