Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stevenspads/b185c80f6ae2b665a1b0e865707ec5e1 to your computer and use it in GitHub Desktop.
Save stevenspads/b185c80f6ae2b665a1b0e865707ec5e1 to your computer and use it in GitHub Desktop.
Get WordPress post archives by Year and then by each Month per year. Display the results with Bootstrap styling.
<?php
global $wpdb;
$limit = 0;
$year_prev = null;
$months = $wpdb->get_results("SELECT DISTINCT MONTH( post_date ) AS month , YEAR( post_date ) AS year, COUNT( id ) as post_count FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'post' GROUP BY month , year ORDER BY post_date DESC");
foreach($months as $month) :
$year_current = $month->year;
if ($year_current != $year_prev)
{
if($year_current != date('Y'))
{
?>
</ul>
<?php
}
?>
<h3>
<a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/"><?php echo $month->year; ?></a>
</h3>
<ul class='list-group'>
<?php
}
?>
<li class='list-group-item'>
<a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>"><span class="archive-month"><?php echo date_i18n("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?></span></a>
&nbsp;<span class='label label-default'><?php echo $month->post_count; ?></span>
</li>
<?php
$year_prev = $year_current;
endforeach;
?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment