WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Count results of wp_list_categories

[Resolved] Count results of wp_list_categories

  • I’m displaying sub-categories on the Categories page – similar to the way contents are displayed on WordPress Codex pages. I’m using blocks with predefined heights to display individual posts within the categories page and floating the “sub-category list” with them. If I don’t specify the height of the list, the boxes are not aligned.

    • I need to specify the height of the “sub-category list”.
      • A default height is defined in the stylesheet.
      • To dynamically adjust the height of the list, I’m using inline styling.
    • To define the height of the list using inline styling (to a pre-defined value), I need to first determine the number of sub-categories in the list (prior to executing wp_list_categories).

    Before, this is what I used and it worked like a champ:

    $currentcat=get_query_var('cat');
    $categories= get_categories('child_of='.$currentcat.'&depth=1');
    $catcount = count($categories);

    $catcount was used to determine the height then specify it with inline styling. Then inside the div I ran:

    foreach ($categories as $cat) {
    $catlist = '<li><a href="'.$cat->category_nicename.'">';
    $catlist .= $cat->cat_name;
    $catlist .= '</a></li>';
    echo $catlist;
    }

    Now I want to exclude child categories – I only want to go 1 level deep with sub-categories in the list.

    So I need to do one of two things from what I can tell:

    1. Count the number of results from wp_list_categories prior to executing it.
    2. OR

    3. Count the number of results (only 1 level deep) using get_categories.

    Here’s the wp_list_categories I’m running:
    wp_list_categories ('title_li=&amp;child_of='.$currentcat.'&amp;depth=1&amp;show_count=1');

    Any suggestions would be appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The ugly way?

    <?php
    $cats=get_categories ();
    $catcount=0;
    foreach ( (array) $cats as $cat ) {
    if ($cat->category_parent=='0') {
    $catcount++;
    }
    }
    ?>

    I changed the ‘0’ in
    if ($cat->category_parent=='0')
    to $currentcat & the ugly way works beautifully.

    Thanks for the solution Michael.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] Count results of wp_list_categories’ is closed to new replies.
Skip to toolbar