Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm looking for how to get a class in the wp_get_archives functions to get the current month (when we are in a month archive) just like when we call wp_list_categories, the current category has a ".current-cat" selector for CSS or when we call wp_list_pages we have a '.current_page_item' selector.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Put the following function in your functions.php

function wpse_62509_current_month_selector( $link_html ) {
    $current_month = date("F Y");
    if ( preg_match('/'.$current_month.'/i', $link_html ) )
        $link_html = preg_replace('/<li>/i', '<li class="current-month">', $link_html );
    return $link_html;
}

And then add the following line just before calling wp_get_archives()

add_filter( 'get_archives_link', 'wpse_62509_current_month_selector' );

You might also want to remove the filter after calling wp_get_archives() so that it doesn't mess with other wp_get_archives() or get_archives_link() function calls.

remove_filter( 'get_archives_link', 'wpse_62509_current_month_selector' );
share|improve this answer
    
Youre on the right track, but what I'm asking is to find the current month of the archives. Maybe I'll try taking it from the URL (www.example.com/2012/08/) to get the values you use. –  Fredy31 Aug 27 '12 at 13:50
    
Well after just a few modifications, that code worked. Thanks! The corrected code is in my answer below. –  Fredy31 Aug 27 '12 at 14:06
add comment

This function was created with the great help of Joshua Abenazer. Thanks! Basically, if is a monthly archive, go and get the current month watched, and add a class on the li. Worked great.

function wpse_62509_current_month_selector( $link_html ) {
    if (is_month()){
        $current_month = get_the_date("F Y");
        if ( preg_match('/'.$current_month.'/i', $link_html ) )
            $link_html = preg_replace('/<li>/i', '<li class="current-month">', $link_html );
    }
    return $link_html;
}

add_filter( 'get_archives_link', 'wpse_62509_current_month_selector' );
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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