Skip to content

Instantly share code, notes, and snippets.

@rhcarlosweb
Last active May 25, 2016 18:40
Show Gist options
  • Save rhcarlosweb/232804c9446434f71c1e4e756466390d to your computer and use it in GitHub Desktop.
Save rhcarlosweb/232804c9446434f71c1e4e756466390d to your computer and use it in GitHub Desktop.

Archive Function

Archive Template Page to WordPress, list years, and months

<ul class="list-archive-years">
<?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_prev != null){?>
<?php } ?>
<li class="archive-year">
<h2>
<a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/"><?php echo $month->year; ?></a>
</h2>
</li>
<?php } ?>
<li>
<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>
</li>
<?php $year_prev = $year_current; if(++$limit >= 18) { break; } endforeach; ?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment