• Resolved firehold

    (@firehold)


    Hello,

    i need a list of all categories with childs.

    News
     - News 1
     - News 2
     - News 3
    
    Posts
     - Posts 1
     - Posts 2
     - Posts 3
     - Posts 4
     - Posts 5
    
    Feedback
     - Feedback 1
     - Feedback 2

    I tried it with

    $categories=get_categories(
        array( 'parent' => $cat->cat_ID )
    );
    foreach ($categories as $c) {
        echo '<li>'.$c->cat_name.'</li>';
    }

    but i just get the parents.

    Thank you very much in advance

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Try
    wp_list_categories( array('hide_empty'=> false ));

    Thread Starter firehold

    (@firehold)

    Thank you very much, this is what i needed.
    Is it possible to output the childs as checkboxes for a form?

    • This reply was modified 5 years, 11 months ago by firehold.
    Thread Starter firehold

    (@firehold)

    UPDATE:
    got it to work.
    Following is my code, is there a better/easier/more effincier way for the same result?

    Thank you very much in advance!!!

    $parent_cats = get_categories( array(
        'orderby' => 'name',
        'parent'  => 0,
    	'hide_empty'=> false
    ) );
    
    echo '<ul style="list-style-type: none;">';
    foreach ( $parent_cats as $pcat ) {
    	echo '<li><label>';
    	echo $pcat->name;
    	echo '</label></li>';
    	
    	$child_cats = get_categories( array(
        'orderby' => 'name',
        'parent'  => $pcat->term_id,
    	'hide_empty'=> false
    	) );
    	if ($child_cats) {
    		echo '<ul style="list-style-type: none;">';
    		foreach ( $child_cats as $ccat ) {
    			if ($ccat) {
    				echo '<li><label>';
    				echo '<input type="checkbox" name="'.$ccat->name.'" value="'.$ccat->term_id.'"> '.$ccat->name;
    				echo '</label></li>';
    			}
    		}
    		echo '</ul>';
    	}
    }
    echo '</ul>';
    Moderator bcworkz

    (@bcworkz)

    No, that looks good. Well done!

    Thread Starter firehold

    (@firehold)

    Thank you /solved

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Output Categories with childs’ is closed to new replies.