Title: Nested Posts Archive
Last modified: August 20, 2016

---

# Nested Posts Archive

 *  Resolved [hotdiggitydog](https://wordpress.org/support/users/hotdiggitydog/)
 * (@hotdiggitydog)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/nested-posts-archive/)
 * Hey guys,
 * I’m trying to pull this off (see html below) without the use of a plugin.
 *     ```
       <ul>
         <li>2012
           <ul>
             <li>December
               <ul>
                 <li>Post title</li>
                 <li>Post title</li>
                 <li>Post title</li>
               </ul>
             </li>
             <li>November
               <ul>
                 <li>Post title</li>
                 <li>Post title</li>
                 <li>Post title</li>
               </ul>
             </li>
           </ul>
         </li>
       </ul>
       ```
   
 * I’ve come up with this snippet of code which currently outputs this:
 * December 2012
    Post title Post title Post title November 2012 Post title Post
   title
 * How can I change the code below so that it replicates my html code? Please someone
   help with this, I don’t think it’s that impossible?
 *     ```
       <?php
   
       // Declare some helper vars
       $previous_year = $year = 0;
       $previous_month = $month = 0;
       $ul_open = false;
   
       // Get the posts
       $myposts = get_posts('numberposts=-1&orderby=post_date&order=DESC&cat=1');
   
       ?>
   
       <?php foreach($myposts as $post) : ?>   
   
           <?php
   
           // Setup the post variables
           setup_postdata($post);
   
           $year = mysql2date('Y', $post->post_date);
           $month = mysql2date('n', $post->post_date);
   
           ?>
   
           <?php if($year != $previous_year || $month != $previous_month) : ?>
   
               <?php if($ul_open == true) : ?>
               </ul>
               <?php endif; ?>
   
               <h3><?php the_time('F Y'); ?></h3>
   
               <ul class="month_archive">
   
               <?php $ul_open = true; ?>
   
           <?php endif; ?>
   
           <?php $previous_year = $year; $previous_month = $month; ?>
   
           <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
   
       <?php endforeach; ?>
           </ul>
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/nested-posts-archive/#post-3411431)
 * I think this will do what you want:
 *     ```
       <?php
   
       function start_new_year($year) {
          echo "\t<li>";
          the_time('Y');
          echo "<ul>\n";
       }
       function end_previous_year() {
          echo "\t</ul></li>\n";
       }
       function start_new_month($month) {
          echo "\t\t<li>";
          the_time('F');
          echo "<ul>\n";
       }
       function end_previous_month() {
          echo "\t\t</ul></li>\n";
       }
   
       // Declare some helper vars
       $previous_year = $year = 0;
       $previous_month = $month = 0;
       $ul_open = false;
   
       // Get the posts
       $myposts = get_posts('numberposts=-1&orderby=post_date&order=DESC&cat=1');
       ?>
   
       <ul>
       <?php $previous_year = $previous_month = '';
   
          foreach($myposts as $post) : ?>
   
          <?php
   
          // Setup the post variables
          setup_postdata($post);
   
          $year = mysql2date('Y', $post->post_date);
          $month = mysql2date('n', $post->post_date);
   
          ?>
   
          <?php if($year != $previous_year) :
             if ($previous_year) :
                end_previous_year();
             endif;
             if ($previous_month) end_previous_month();
             start_new_year($year);
             start_new_month($month);
             $previous_year = $year;
             $previous_month = $month;
          endif;
          if ($month != $previous_month) :
             if ($previous_month) :
                end_previous_month();
             endif;
             start_new_month($month);
             $previous_month = $month;
          endif;
          ?>
   
           <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
   
       <?php endforeach;
       end_previous_month();
       end_previous_year(); ?>
   
       </ul>
       ```
   
 *  Thread Starter [hotdiggitydog](https://wordpress.org/support/users/hotdiggitydog/)
 * (@hotdiggitydog)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/nested-posts-archive/#post-3411439)
 * Excellent, thank you very much.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Nested Posts Archive’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [hotdiggitydog](https://wordpress.org/support/users/hotdiggitydog/)
 * Last activity: [13 years, 3 months ago](https://wordpress.org/support/topic/nested-posts-archive/#post-3411439)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
