WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Nested Posts Archive (3 posts)

  1. hotdiggitydog
    Member
    Posted 1 year ago #

    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>
  2. vtxyzzy
    Member
    Posted 1 year ago #

    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>
  3. hotdiggitydog
    Member
    Posted 1 year ago #

    Excellent, thank you very much.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags