WordPress.org

Support

Support » How-To and Troubleshooting » Looping through / Grouping by Sub-Categories

Looping through / Grouping by Sub-Categories

  • I have posts under the following category structure:

    Destinations
    — Alaska (5 posts)
    — Antarctica (2 posts)
    — Arctic (0 posts)
    — Asia (1 posts)
    etc. etc.

    I’m looking to create a custom loop that “loops” through all the sub-categories (under Destinations), displays the category name (if posts exist under that category – as a header), then the post titles under that category.

    Example of my expected code output:

    <section>
       <h2>Alaska</h2>
       <ul>
       <li>Alaska post #1</li>
       <li>Alaska post #2</li>
       <li>Alaska post #3</li>
       <li>Alaska post #4</li>
       <li>Alaska post #5</li>
       </ul>
    
       <h2>Antarctica</h2>
       <ul>
       <li>Antarctica post #1</li>
       <li>Antarctica post #2</li>
       </ul>
    
       <h2>Asia</h2>
       <ul>
       <li>Asia post #1</li>
       </ul>
    
    ...etc...
    </section>

    I prefer to handle this via a loop and not another plug-in. Any assistance is much appreciated.

    Thanks.

    — Dennis

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Looping through / Grouping by Sub-Categories’ is closed to new replies.
Skip to toolbar