WordPress.org

Forums

Nested year, month and date archive navigation. (6 posts)

  1. Schalken
    Member
    Posted 6 years ago #

    How can I generate a nested list of archive links by year, month and date like the following? Using WP 2.7.

  2. krembo99
    Member
    Posted 6 years ago #

  3. Schalken
    Member
    Posted 6 years ago #

    That could be used if I wanted to cover every single day of every single of every single month of every single year, but I only want to show links for days/months/years that have posts in them.

    Surely there is a way?

  4. krembo99
    Member
    Posted 6 years ago #

    I did not say you should put it directly,
    I said, do a loop function to filter posts, using those hooks to display links.

  5. haveboard
    Member
    Posted 5 years ago #

    <ul>
    <?php
    /**/
    $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date DESC");
    foreach($years as $year) :
    ?>
    	<li><a href="<?php echo get_year_link($year); ?> "><?php echo $year; ?></a>
    
    		<ul>
    		<?	$months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND YEAR(post_date) = '".$year."' ORDER BY post_date DESC");
    			foreach($months as $month) :
    			?>
    			<li><a href="<?php echo get_month_link($year, $month); ?>"><?php echo date( 'F', mktime(0, 0, 0, $month) );?></a>
    				<ul>
    				<?	$days = $wpdb->get_col("SELECT DISTINCT DAY(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND MONTH(post_date) = '".$month."' AND YEAR(post_date) = '".$year."' ORDER BY post_date DESC");
    					foreach($days as $day) :
    					?>
    					<li><a href="<?php echo get_day_link($year, $month, $day); ?>"><?php echo $day;?></a></li>
    					<?php endforeach;?>
    				</ul>
    
    			</li>
    			<?php endforeach;?>
    		</ul>
    	</li>
    <?php endforeach; ?>
    </ul>

    This should do the trick. I was looking for the same answer myself and figured I'd add this up. There may be a more efficient way to do this, but that's what I used to get it working.

  6. boonesimpson
    Member
    Posted 5 years ago #

    This code is really great, is there a way to add an additional layer with post titles?

    • 2008
      • January
        • 15th
          • Post title 1
          • Post title 2
        • 17th
          • Post title 1
          • Post title 2

Topic Closed

This topic has been closed to new replies.

About this Topic