-1

I'm trying to figure out how to get the following archive navigation:

JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC 2012 | 2014

Thus, a list with the months of the current or selected year with links to the previous and next year's archive.

I've tried to edit the Compact Archives plugin, which comes very close to what I need. It displays the months for all years, including those that don't have posts. However, I cannot get it to display the months for the current/selected year only. I know this has to do with the for each statement, but my knowledge of php is not that good that I can make the necessary customizations for it to work.

This is what I have customized thus far:

    function get_compact_archive( $style='initial', $before='<li>', $after='</li>' ) {
global $wpdb, $wp_version;
setlocale(LC_ALL,WPLANG); // set localization language
$below21 = version_compare($wp_version, '2.1','<');
// WP 2.1 changed the way post_status and post_type fields work
if ($below21) {
    $now = current_time('mysql');
    $results = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month FROM " . $wpdb->posts . " WHERE post_date <'" . $now . "' AND post_status='publish' AND post_password='' ORDER BY year DESC, month DESC");
} else {
    $results = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month FROM " . $wpdb->posts . " WHERE post_type='post' AND post_status='publish' AND post_password='' ORDER BY year DESC, month DESC");
}
if (!$results) {
    return $before.__('Archive is empty').$after;
}
$dates = array();
foreach ($results as $result) {
    $dates[$result->year][$result->month] = 1;
}
unset($results);
$result = '';
foreach ($dates as $year => $months){
    $prev_year = $year - 1;
    $next_year = $year + 1;
    $result .= $before . '<ul class="archive-navigation-months">';
    for ( $month = 1; $month <= 12; $month += 1) {
        $month_has_posts = (isset($months[$month]));
        $dummydate = strtotime("$month/01/2001");
        // get the month name; strftime() localizes
        $month_name = strftime("%B", $dummydate); 
        switch ($style) {
        case 'initial':
            $month_abbrev = $month_name[0]; // the inital of the month
            break;
        case 'block':
            $month_abbrev = strftime("%b", $dummydate); // get the short month name; strftime() localizes
            break;
        case 'numeric':
            $month_abbrev = strftime("%m", $dummydate); // get the month number, e.g., '04'
            break;
        default:
            $month_abbrev = $month_name[0]; // the inital of the month
        }
        if ($month_has_posts) {
            $result .= '<li><a href="'.get_month_link($year, $month).'" title="'.$month_name.' '.$year.'">'.$month_abbrev.'</a></li>';
        } else {
            $result .= '<li class="emptymonth">'.$month_abbrev.'</li>';
        }
    }
    $result .= '</ul><ul class="archive-navigation-years"><li><a href="'.get_year_link($prev_year).'">'.$prev_year.'</a></li><li><a href="'.get_year_link($next_year).'">'.$next_year.'</a></li></ul>' . $after."\n";
}
return $result;

}

I've also checked this thread but that doesn't help me either.

  • If this is a php question then this is probably not the best place to ask it. – Mark Kaplun Jan 1 '14 at 6:10
0

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();

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.