Skip to content

Instantly share code, notes, and snippets.

@evalcraciun
Created September 24, 2014 18:48
Show Gist options
  • Save evalcraciun/21dcdd3cd3cff3c93468 to your computer and use it in GitHub Desktop.
Save evalcraciun/21dcdd3cd3cff3c93468 to your computer and use it in GitHub Desktop.
Wordpress Custom Archive
<?php
$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){?>
</ul>
<?php } ?>
<li><a class="title unhide" href="#"><?php echo $month->year; ?></a>
<ul class="archive-sub-menu">
<?php } ?>
<li>
<a href="#" class="unhide">
<span class="archive-month"><?php echo date("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?></span>
<span class="archive-count">( <?php echo $month->post_count; ?> )</span>
</a>
<ul class="archive-sub-menu">
<?php $theids = $wpdb->get_results("SELECT DISTINCT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND MONTH(post_date)= '".$month->month."' AND YEAR(post_date) = '".$month->year."' ORDER BY post_date ASC");
foreach ($theids as $theid):
?>
<li>
<a href="<?php bloginfo('url') ?>/?p=<?php echo $theid->ID; ?>"><?php echo $theid->post_title; ?></a>
</li>
<?php endforeach; ?>
</ul>
</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