0

we have wp_get_archives('type=monthly'); function that have a output like this:

Archives by Month:
January 2014
December 2013
November 2013

but I want style like a table that sorted by years:

2014
January
-------------
2013
December    November
0

1 Answer 1

2

The only filter i could find inside the wp_get_archives function is for displaying the links. Based on the get_archives_link filter, this should work, use it in your functions.php file:

$archive_year = 0;
add_filter('get_archives_link','wp_get_archive_grouped_by_year',10,2);
function wp_get_archive_grouped_by_year($link_html) {
    global $archive_year;
    //Get the year from the link(probably better if you change this to regexp)
    $year_new = explode(' ', $link_html);
    $year_new = explode('</a>',$year_new[2]);
    $year_new = $year_new[0];
    $year_html = '';

    //If the year is not display previously
    if ($year_new != $archive_year) {
        $archive_year = $year_new;
        $year_html = '<li class="year">'.$archive_year.'</li>';
    }

    //Remove the year from the month link
    $link_html = str_replace(' '.$archive_year, "", $link_html);

    //Return the new links and exclude a specific year:
    if($archive_year != '2014') {
        echo $year_html.$link_html;
    }
}

Results:

<li class="year">2014</li>
<li><a href='#'>January</a></li>
<li class="year">2013</li>
<li><a href='#'>November</a></li>
<li><a href='#'>April</a></li>
1
  • 1
    Check out the code again, it will exclude all of the posts from 2014
    – passatgt
    Commented Jan 19, 2014 at 12:53

Your Answer

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

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