treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Group query_posts results by month?

  • Hi,

    So I'm working on the archive section for a Wordpress blog, and am using query_posts to display all the posts and also using some pagination. However I'd like to show the posts grouped by month. So like :

    September 2012: - Post 1 - Post 2

    August 2012 - Post 1 - Post 2

    Etc.

    Here's the code I'm using : (The list is there 2 times because as you may see I've added the functionality of switching between an image viewer and a list viewer.)

      <?php query_posts('&posts_per_page=18'); ?>
    
    
      <ul class="posts hide list" id="post-<?php the_ID(); ?>" data-display-type="list">
      <?php if(have_posts()) : while ( have_posts() ) : the_post(); ?>
    
        <li class="post">
    
          <header class="post-header">
            <h4 class="no-margin"><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h4>
            <p class="date no-margin small black arial"><?php the_time(get_option('date_format')); ?></p>
          </header>
          <div class="post-excerpt">
            <p class="no-margin">...<?php echo substr(strip_tags(get_the_content($post->ID)), 0, 200); ?>...</p>
          </div>
        </li>
      <?php endwhile; endif; ?> 
      </ul>
    
      <ul class="posts clearfix grid" data-display-type="grid">
      <?php if(have_posts()) : while ( have_posts() ) : the_post(); ?>
        <li class="post left <?php echo $columns[$i % 3];?>">
          <a href="<?php the_permalink(); ?>" class="overlay-btn">
            <div class="overlay semi-black-bg">
              <h5 class="title didot-italic text-center white"><?php the_title(); ?></h5>
            </div>
            <?php the_post_thumbnail(array('154', '999')); ?>
          </a>
        </li>
      <?php endwhile; endif; ?> 
      </ul>
    

    Anyone have any ideas of how to group the results by the month they were published? Thanks in advance :)