wordpress categories and their subcategories

Posted by Ayrton on Stack Overflow See other posts from Stack Overflow or by Ayrton
Published on 2010-04-05T11:39:15Z Indexed on 2010/04/05 11:43 UTC
Read the original article Hit count: 346

Filed under:

Hi I'm looking to obtain all the (parent) categories with their children accordingly (assuming you don't have grandchildren) and make the following structure

  <div class="box">
    <h3><a href="#">Parent Category I</a></h3>
    <ul>
      <li><a href="#">sub cat 1</a></li>
      <li><a href="#">sub cat 2</a></li>
      <li><a href="#">sub cat 3</a></li>
    </ul>
  </div>
  <div class="box">
    <h3><a href="#">Parent Category II</a></h3>
    <ul>
      <li><a href="#">sub cat 1</a></li>
      <li><a href="#">sub cat 2<</a></li>
      <li><a href="#">sub cat 3</a></li>
    </ul>
  </div>

I figured it's something like (don't mind the syntax) however I don't know how to obtain those (parent)categories and their children:

$parents = ... ;

foreach($parents as $parent){
<div>
<h3>$parent</h3>

$children = ...;
<ul>
foreach ($children as $child){
<li>$child</li>
}
</ul>

</div>

}

© Stack Overflow or respective owner

Related posts about Wordpress