0

I am using the get_categories() function combined with a foreach loop, to create a list of all the catgorys in a form. like so:

$args = array(
                'type'                     => 'post',
                'hide_empty'               => 0, //<--IMPORTANT!!
                'hierarchical'             => 1,
                'taxonomy'                 => 'category',
                'pad_counts'               => false );
                $categories = get_categories($args);
                foreach($categories as $category) { 
                        echo '<tr>';
                        echo "<td>$category->name</td>";
                        echo "<td><input type='text' name='$category->cat_ID'/></td>";
                        echo '</tr>';
                    }

However, what I need is for all the sub categories to be indented, but I cannot work out how.

After looking at the documentation Here it seems that $category->parent parameter, may be helpful, but I cannot get it working.

Any help would be appreciated.

2
  • You will have to add the indents manually, WordPress supports indents for Category Drop Downs only.
    – tamilsweet
    Jun 6, 2012 at 0:32
  • Looks like a typical usecase for wp_list_categories. Why are you not using it? Jun 6, 2012 at 9:04

1 Answer 1

1
$args = array(
    'type' => 'post',
    'hide_empty' => 0,
    'hierarchical' => 1,
    'taxonomy' => 'category',
    'pad_counts' => false
);
$categories = get_categories($args);

foreach($categories as $category) { 

    echo '<tr>';

    if($category->parent != 0){ // If this is a subcategory
        echo "<td>&nbsp;&nbsp;$category->name</td>";
    } else {
        echo "<td>$category->name</td>";
    }

    echo "<td><input type='text' name='$category->cat_ID'/></td>";
    echo '</tr>';

}
2
  • Please mark this answer as accepted, if it's how you solved your problem.
    – MathSmath
    Jun 6, 2012 at 14:55
  • I will do as soon as the waiting period is over.
    – 1321941
    Jun 6, 2012 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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