0

Here's code I've modified to display an archive list of every month I've posted news. Each month of course links to the archive for that month. I can then include the shortcode wherever I like.

function my_archives($params, $content = null) {

extract(shortcode_atts(array(
'type' => 'style1'
), $params));

ob_start();
?>

<?php wp_get_archives( array( 'type' => 'monthly' ) ); ?>     

<?php return ob_get_clean();
}
add_shortcode('archives','my_archives');

However, I just cannot figure out from reading the wp_get_archives page (http://codex.wordpress.org/Function_Reference/wp_get_archives) how to organize the list of months with, say, a year heading. So right now the above code outputs like this:

<li>June 2014</li>
<li>May 2014</li>
<li>April 2014</li>
etc. etc. etc. for every year and month (my website has been up for 11 years, so that's over 100 links in one blob of a list).

I was wondering if it is even possible to change the list to something like this instead:

<b><u>2014</u></b><ul>
<li>June 2014</li>
<li>May 2014</li>
etc.</ul>
<b><u>2013</u></b><ul>
<li>December 2014</li>
<li>November 2014</li>
etc.</ul>

Thank you!

2 Answers 2

1

I found this answer: http://www.stemlegal.com/strategyblog/2011/wordpress-wednesdays-better-archive-lists-in-wordpress/

And the final code looks something like this:

function getarchives_filter($where, $args) {
    if (isset($args['year'])) {
    $where .= ' AND YEAR(post_date) = ' . intval($args['year']); }
    return $where;
}
add_filter('getarchives_where', 'getarchives_filter', 10, 2);

function my_archives($params, $content = null) {

    extract(shortcode_atts(array(
    'type' => 'style1'
    ), $params));

    $currentyear = date('Y');
    $years = range('2012',$currentyear);
    foreach($years as $year){
        $archive = wp_get_archives( array( 'type' => 'monthly','echo' => 0 ,'year' => $year) );
        if(!empty($archive)){
            echo '<ul>';
            echo '<li>'.$year.'</li>';
            echo $archive;
            echo '</ul>';
        }
    }

}
add_shortcode('archives','my_archives');
1

This will output months with a year heading. 11 years is a lot, in the past I've added a simple jQuery .slideToggle(), whereby if a user clicks a year they then get the list of months for that year. Just a suggestion.

  <?php
    $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date DESC");
    foreach($years as $year) {
    ?>

      <a class="year" href="<?php echo get_year_link($year); ?>"><?php echo $year; ?></a>
        <?php   $months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND YEAR(post_date) = '".$year."' ORDER BY post_date DESC");
            foreach($months as $month) {
        ?>
          <a href="<?php echo get_month_link($year, $month); ?>"><?php echo date( 'F', mktime(0, 0, 0, $month) );?></a>
        <?php } ?>

  <?php } ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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