Need WordPress Archive to display in a certain format

802 views Asked by At

my WordPress blog currently shows archives in a month by month format. Like this:

November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
.....
and so on

I need it to display in such a way that the current year shows all months, but for all the years before that, it only shows the "Year". Something like this:

November 2014
October 2014
September 2014
...
...
February 2014
January 2014
2013
2012
2011
2010

Can someone please guide me on this? The current code in my blog's sidebar goes like this:

<?php /* If this is a category archive */ if ( is_category(0) || in_category(0)) { ?>
<?php ?><ul>
<?php
$querystr = "SELECT YEAR(post_date) AS 'year', MONTH(post_date) AS 'month' , count(ID) as posts FROM $wpdb->posts INNER JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE $wpdb->term_taxonomy.term_id != 12 AND $wpdb->term_taxonomy.parent != 12 AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id =8 AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC";

$years = $wpdb->get_results($querystr);

foreach ( (array) $years as $year ) {
    $url = get_month_link($year->year, $year->month );
    $url = $url.'?cat=8';
    $date = mysql2date('F o', $year->year.'-'.$year->month, $translate = true);
    echo get_archives_link($url, $date, 'html','<li>','</li>');
}
?>
</ul><?php 

?>

<?php } else { ?>
            <?php wp_get_archives('type=monthly');?>

<?php } ?>



                </ul>






                         </div>
                           <div class="archive-bottom"></div>
                <!--    <div class="clear"></div>-->
 <?php } ?>                   




</div>
1

There are 1 answers

1
rnevius On BEST ANSWER

You're currently using nothing other than wp_get_archives('type=monthly'), which is obviously causing the issue. You should have a look at the wp_get_archives() docs in the Codex.

The following is a sample solution, but will likely need a bit of work (it assumes you're posting every month this year, as shown in your post). It hasn't been tested, so use at your own risk.

// Get the current month number
$current_month = date('n');

// Show the archives for the current year, by month, 
// by limiting the returned archives to the current month number
$args = array(
    'limit' => $current_month
);
wp_get_archives( $args );


// Awesome, now let's get the previous years, but leave off this year
$yearly_archives = wp_get_archives('type=yearly&echo=0'); 
// Split the returned archives at this year...
$old_archives = explode('</li>', $yearly_archives, 2);
// And only echo list items that don't include this year.
echo $old_archives[1];