• Resolved bechster

    (@bechster)


    Hi,

    I’m trying to make a theme which shows an overview of child categories with title, link and description when entering a category archive. However, I only want to show child categories one level below the current category, and not the children of child categories. How do I do that?

    I’ve tried the following:

    $childcategories = get_categories('child_of=' . $cat . '&hide_empty=1');
    foreach ($childcategories as $childcategory) {
      if (in_category($childcategory)) {
        echo '<li><h2><a href="';
        bloginfo('url');
        echo '/category/'.$cat_id->category_nicename.'/'.$childcategory->category_nicename.'">';
        echo $childcategory->cat_name . '</a></h2>';
        echo '<p>'.$childcategory->category_description.'</p>';
        echo '<p>'.$childcategory->cat_ID.'</p>';
        echo '</li>';
      }
    }

    … which seems to work perfectly in MAMP for mac but fails in XAMPP for win as well as on my debian server.

    What am I doing wrong? Can anybody help?

    Thanks in advance.

Viewing 5 replies - 1 through 5 (of 5 total)
  • See if the depth=n argument works. If works for wp_list_categories so guessing it might work for get_categories.

    Thread Starter bechster

    (@bechster)

    Thanks for the tip, however it didn’t work.

    Thread Starter bechster

    (@bechster)

    mthomps found the solution: https://www.ads-software.com/support/topic/223988?replies=9#post-1054625

    I ended up with this piece of code, which works as intended:

    <?php
    global $ancestor;
    $childcats = get_categories('child_of=' . $cat . '&hide_empty=1');
    foreach ($childcats as $childcat) {
      if (cat_is_ancestor_of($ancestor, $childcat->cat_ID) == false){
        echo '<li><h2><a href="'.get_category_link($childcat->cat_ID).'">';
        echo $childcat->cat_name . '</a></h2>';
        echo '<p>'.$childcat->category_description.'</p>';
        echo '</li>';
        $ancestor = $childcat->cat_ID;
      }
    }
    ?>

    Thanks for the info Bechster. It is useful when you want to show a specialized list of subcategories on your parent category page that includes description.

    I am also combining it with the category image technique here:

    <?php
    foreach((get_the_category()) as $category) {
        echo '<img src="https://example.com/images/' . $category->cat_ID . '.jpg" alt="' . $category->cat_name . '" />';
    }
    ?>

    to make my parent category more of an overview page.

    Thanks a lot. love the wordpress community.

    thanks bechster
    this is perfect for what i needed
    i am using the just one category plugin and wanted to display a list of subcategories when there are no posts in the parent category.
    i placed this in the archives.php file to show when there are no posts for that category
    (ie parent will have no posts but wanted it to show subcategories and not no posts message)
    it works perfectly
    thanks again

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How do I get the category children without getting the children of children?’ is closed to new replies.