Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
You will have to add the indents manually, WordPress supports indents for Category Drop Downs only. –  tamilsweet Jun 6 '12 at 0:32
    
Looks like a typical usecase for wp_list_categories. Why are you not using it? –  KDM Jun 6 '12 at 9:04

1 Answer 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>';

}
share|improve this answer
    
Please mark this answer as accepted, if it's how you solved your problem. –  MathSmath Jun 6 '12 at 14:55
    
I will do as soon as the waiting period is over. –  Mr D Jun 6 '12 at 16:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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