WordPress.org

Support

Support » Themes and Templates » Nesting posts by month in custom Archive template

Nesting posts by month in custom Archive template

  • Hi there,

    I’ve used the following code to create this custom page: Archive page

    <?php 	$month = '';
    		$prevmonth = '';
    		$year = '';
    		$prevyear = ''; ?>
    
    <?php query_posts('showposts=-1'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    	<?php $year = mysql2date('Y', $post->post_date); ?>
    	<?php $month = mysql2date('F', $post->post_date); ?>
    
    	<?php if ($month != $prevmonth) { ?>
    
    		<h4><?php echo $month . ' ' . $year; ?></h4>
    		<?php $prevmonth = $month; ?>
    
    	<?php } elseif ($year != $prevyear) { ?>
    		<h4><?php echo $month . ' ' . $year; ?></h4>
    		<?php $prevmonth = $month; ?>
    		<?php $prevyear = $year; ?>
    	<?php } ?>
    
    	<p><a href="<?php the_permalink(); ?>"><?php echo mysql2date('d.m.y', $post->post_date); ?> - <?php the_title(); ?></a></p>
    
    <?php endwhile; endif; ?>

    As you can see by the link above, the formatting is rather baggy and some months repeated. I would like there to be line spaces between the date name headers but the nested posts to appear in a tighter list without space.

    Any ideas how to stop months such as December 2012 and April 2013 appearing twice and tighten up the formatting?

    Thanks!

  • The topic ‘Nesting posts by month in custom Archive template’ is closed to new replies.
Skip to toolbar