Generate a year -> month based list of posts in WordPress, similar to the native Archives widget, only with months being children of years as oposed to a flat list
<?php // In functions.php:
/* -- Year and month based archive listings -- */
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' => 'national-press',
'post_status' => 'publish'
));
// loop through posts, populating $years arrays
foreach($posts as $post) {
$years[date('Y', strtotime($post->post_date))][date('F', strtotime($post->post_date))] = $post;
}
// reverse sort by year
krsort($years);
return $years;
}
?>
<?php // and in your theme file:
// All of the extra classes on <li>'s are to mimic the output of WP's native menus
foreach(posts_by_year() as $year => $posts) : ?>
<li id="" class="main-menu-item menu-item menu-item-has-children <?php echo ((int)$get_year == (int)$year ? 'current-page-ancestor current-menu-ancestor current-menu-parent current-page-parent current_page_parent current_page_ancestor current-menu-item' : ''); ?>">
<a href="<?php echo site_url('/news-press/national-press/') . $year . "/"; ?>" class="toggle-link menu-link main-menu-link"><?php echo $year; ?></a>
<ul class="sub-menu">
<?php foreach($posts as $key => $val) : ?>
<?php $is_current_page = is_current_page(site_url('/news-press/national-press/') . $year . "/" . $key . "/"); // is_current_page() function also in Gist repo :) ?>
<li id="" class="sub-menu-item menu-item <?php echo $is_current_page ? "current-menu-item" : ""; ?>">
<a href="<?php echo site_url('/news-press/national-press/') . $year . "/" . $key . "/"; ?>" class="menu-link sub-menu-link"><?php echo $key; ?></a>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>