WordPress.org

Forums

[resolved] Looping thru Categories to get child divs containing grandchildren lists (3 posts)

  1. derker
    Member
    Posted 4 years ago #

    Hi,
    thanks for looking...
    Ok so I am trying to loop thru my Q&A category and echo divs of the child categories within (lettters A-Z, for example A) which would then be filled with an unordered list of it's children (Acne, Asthma, Aches,) aka the grandchildren of Q&A... so i would end up with something like this:

    <div id="A">
      <ul>
       <li><a href="link to acne">Acne</a></li>
       <li><a href="link to...">Asthma</a></li>
       <li><a href="link to...">Aches</a></li>
      </ul>
    </div>
    <div id="B">
      <ul>
       <li><a href="link to boils">Boils</a></li>
       <li><a href="link to...">Blisters</a></li>
       <li><a href="link to...">Burns</a></li>
      </ul>
    </div>
    <div id="C">
      <ul>
       <li><a href="link to cramps">Colds</a></li>
       <li><a href="link to...">Coughs</a></li>
       <li><a href="link to...">Cramps</a></li>
      </ul>
    </div>
    etc... on thru A-Z (all the children of Q&A)

    All I can get going now is to print out all the grandchildren but this is not really that close to what I would like to get (ie. above code) I'm a lil confused on how to print out the grandchildren under the child and then move on to the next child and do the same...
    Any help would be much appreciated...
    here's where i'm stuck:

    <?php
    			$categories=get_categories('parent=4&hide_empty=0');
    			  foreach($categories as $category) {
    			    $subcats = get_categories('child_of='.$category->term_id);
    				if($subcats) {
    				    foreach( $subcats as $subcat )
    				     echo '<p><a href="' . get_category_link( $subcat->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $subcat->name ) . '" ' . '>' . $subcat->name.'</a> </p> ';
    				  }
    			      }
    			?>
  2. derker
    Member
    Posted 4 years ago #

    getting closer... this code doesn't work, i think it i may need the "chain" but i'm not too familiar....any help much appreciated

    <?php
    $categories=get_categories('parent=4&hide_empty=0');
    if($categories) {
    foreach($categories as $cat) {
    $option = '<div class="'.$cat->category_nicename.'"><ul>';
    $subcats = get_categories('child_of='.$cat->term_id);
    $kits = array();
    if($subcats) {
    foreach( $subcats as $subcat )
    $option .= '<li><a href="';
    $option .= get_category_link($subcat->term_id);
    $option .= '"> title="';
    $option .= sprintf( __( "View all posts in %s" ), $subcat->name );
    $option .= '"> '$subcat->name'';
    $option .= '</a></li>';
    }
    $option .= '</ul></div>';
    echo $option;
    }
    }
    ?>
  3. derker
    Member
    Posted 4 years ago #

    boy do i feel like a bonehead... sometimes it helps to look under your nose and not over think it...

    here's the solution i figured out myself... yay! ;)

    <?php
    $categories=get_categories('parent=4&hide_empty=0');
    foreach($categories as $cat) {
    echo '<div class="' . $cat->category_nicename . '" ' . '>' . '<ul>';
    wp_list_categories('title_li=&child_of='.$cat->term_id);
    echo '</ul></div>';
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic