Sunday, 2 June 2013

Show all 12 months regardless they have posts

Show all 12 months regardless they have posts

I need to show all months for a year for an archives page. All 12 months must be shown regardless of whether or not there are posts published for a month. Like this:
Year: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Of course, months with published posts are links. Months with no published posts are simply the word
This is my code:
<ul>

<?php
    global $wpdb;
    $limit = 0;
    $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){ ?>

    <?php } ?>

    <li> <a class="year" href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/">> <?php echo $month->year; ?></a>

    <?php } ?>

    <a class="month" href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>"><?php echo date_i18n("M", mktime(0, 0, 0, $month->month, 1, $month->year)) ?></a>

    <?php $year_prev = $year_current; if(++$limit >= 10) { break; } endforeach; ?></li>
</ul>
My achieve what I want to do except for showing all the 12 months.

No comments:

Post a Comment