Skip to content

Instantly share code, notes, and snippets.

@mavieth
Created January 13, 2016 20:12
Show Gist options
  • Select an option

  • Save mavieth/14b27120201f479eda7c to your computer and use it in GitHub Desktop.

Select an option

Save mavieth/14b27120201f479eda7c to your computer and use it in GitHub Desktop.
Wordpress-Archive-By-Year
Page Template
<?php
/**
* Template Name: Press By Year
*/
get_header(); ?>
<div id="content-full" class="grid col-940">
<?php foreach(posts_by_year() as $year => $posts) : ?>
<h3><?php echo $year; ?></h3>
<ul>
<?php foreach($posts as $post) : setup_postdata($post); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
</div><!-- end of #content-full -->
<?php get_footer(); ?>
// functions.php
function posts_by_year() {
// array to use for results
$years = array();
// get posts from WP
$posts = get_posts(array(
'numberposts' => -1,
'orderby' => 'post_date',
'order' => 'ASC',
'post_type' => 'press-release',
'post_status' => 'publish'
));
// loop through posts, populating $years arrays
foreach($posts as $post) {
$years[date('Y', strtotime($post->post_date))][] = $post;
}
// reverse sort by year
krsort($years);
return $years;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment