Abban Dunne, Web Design and Development

Add A Current Class To wp_get_archives.


No Items

Sometimes I like to use wp_get_archives or the archive widget for a secondary navigation. This lets you list posts organised by month. The only problem with it is it doesn’t highlight the current active item. To overcome this I wrote this little jQuery function:

$(function(){
    var date = <?php echo get_the_date('F Y'); ?>';
    $('.widget a[title="'+date+'"]').addClass('current');
});

So what’s happening here is its I’m setting a variable called date and setting it with a WordPress function called get_the_date(); This sets it to the current template date. It then looks through your widget for the item with the title attribute set to that date. Just remember that I wrap my widgets in a div with a class widget so if you use a different class you need to change it to the appropriate one.


Comments

Thanks for this post! It works perfectly.

Just want to add if you want the class on the tag so it works like categories, you can do this:

$(‘.widget a[title="'+date+'"]‘).parent().addClass(‘current’);

Also, this javascript needs to go below the queue for the first post in your archive.php template. So after:

if ( have_posts() )
the_post();


Leave a Reply

Your email address will not be published. Required fields are marked *