• I need to display first children level of current category (if any), but not children of children.

    For example:
    Main category (1st level)
    – subcategory one (2nd level)
    – subcategory two (2nd level)
    – some category (3rd level)
    – other category (3rd level)
    – some category (4th level)

    When on “Main category (1st level)” I wish to show just:
    “subcategory one (2nd level)”
    “subcategory two (2nd level)”.

    When on “subcategory one (2nd level)” I wish to show just:
    “some category (3rd level)”
    “other category (3rd level)”

    So, I wish just children of current category (not its grandchildren).

    This is code I am using, it works, but it shows all children with its sub children…

    <?php
    $args=array(
        'child_of' => $cat-id,
        'hide_empty' => 0,
        'orderby' => 'name',
        'order' => 'ASC'
    
    );
    $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>';  }
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Advanced SEO

    (@jole5)

    I tried with:

    'depth' => '1'

    but it breaks theme.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Maybe the syntax was wrong, it shouldn’t break your theme. Can you post the code with the depth of 1?

    Thread Starter Advanced SEO

    (@jole5)

    <?php
    $args=array(
        'child_of' => $cat-id,
        'hide_empty' => 0,
        'orderby' => 'name',
        'order' => 'ASC',
        'depth' => '1'
    
    );
    $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>';  }
    ?>

    This is complete code. It still shows all children and its children…

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Show category children, only first children level of curent category/subacategy’ is closed to new replies.