I left the plugin for what it was and finally figured it out with help of one of the answers in the other thread I’ve mentioned. The code below did what I needed.
function get_posts_grouped_by_month( $year = null ) {
if ( $year == null ) {
$year = date('Y');
}
$months = range(1,12);
$posts = array();
foreach ( $months as $month ) {
$posts_for_month = get_posts(array(
'year' => $year,
'monthnum' => $month ));
$posts[$month] = $posts_for_month;
}
return $posts;
}
function compacter_archives() {
$current_year = get_the_date( 'Y' );
$previous_year = $current_year - 1;
$next_year = $current_year + 1;
$monthly_posts = get_posts_grouped_by_month($current_year);
if ( function_exists('icl_get_languages') ) {
global $sitepress;
$current_lang = $sitepress->get_current_language();
} else {
$current_lang = get_locale();
}
echo '<ul class="compacter-archives-month-list">';
foreach ( $monthly_posts as $month => $posts ) {
setlocale(LC_TIME, $current_lang);
$time = mktime(0, 0, 0, $month);
$month_name = strftime("%b", $time);
if ($posts) {
echo '<li><a href="' . get_month_link( $current_year, $month ) . '">' . $month_name . '</a></li>';
} else {
echo '<li>' . $month_name . '</li>';
}
}
echo '</ul><ul class="compacter-archives-year-list">';
$query1 = new WP_Query( array ( 'year'=> $previous_year ) );
if ( $query1->have_posts() ) {
echo '<li><a href="' . get_year_link($previous_year) . '">' . $previous_year . '</a></li>';
} else {
echo '<li>' . $previous_year . '</li>';
}
wp_reset_query();
$query2 = new WP_Query( array ( 'year'=> $next_year ) );
if ( $query2->have_posts() ) {
echo '<li><a href="' . get_year_link($next_year) . '">' . $next_year . '</a></li>';
} else {
echo '<li>' . $next_year . '</li>';
}
wp_reset_query();
}