Forum Moderators: open

Message Too Old, No Replies

How To Modify 'wp get archives' in WordPress

need to highlight current archive category

         

luispunchy

1:20 pm on Apr 23, 2008 (gmt 0)

10+ Year Member



Running WordPress v2.5

I call wp_get_archives in a sidebar, using the defaults so it displays archives listed by month. I want to highlight the month in the list when you are on that month's archive page.

This would be similar to the logic with wp_list_categories when the category list is generated on a Category Archive page, and how the list item for that category is marked with the HTML class current-cat.

I know the XHTML/CSS involved (i.e., I know how to style the markup to get that affect) - but I CANNOT figure out how to coerce WordPress to generate the necessary class attribute for me to use as a style hook.

I need something like this:


<ul id="archives">
<li class="selected"><a href="#" title="April 2008">April 2008</a></li>
<li><a href="#" title="March 2008">March 2008</a></li>
<li><a href="#" title="Feb 2008">Feb 2008</a></li>
...
</ul>

How do I get wp_get_archives to generate a class attribute for the selected archive month?

ergophobe

3:54 pm on Apr 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't know how to do it without modifying the WP core code, which I think is a bad idea. I'm sure it can be done, but I don't see any filter hooks [codex.wordpress.org] for archives in the plugin API Filter Reference [codex.wordpress.org].

Like I say, I'm sure it can be done with a plugin, I just don't know how.

If you want to live dangerously and hack the Wordpress code (and then invite upgrade hell), basically you would need to get into the wp_get_archives() function in the general-template.php file.

instead of line 417 (2.5.1)


echo get_archives_link($url, $text, $format, $before, $after);

you would want something like (this is untested)


if ($url == $_SERVER['REQUEST_URI'])
{
$before = $before . '<span class="current">';
$after = '</span>' . $after;
}
echo get_archives_link($url, $text, $format, $before, $after);

That's just off the top of my head - I'm not sure what format the $url variable has, so you would have to output it and see if you need to do some additional processing before comparing it to $_SERVER['REQUEST_URI']

I don't know if that helps. If you can find someone who really knows the plugin API, I'm sure they could do it smarter.

luispunchy

7:14 pm on Apr 23, 2008 (gmt 0)

10+ Year Member



@ergophobe - thanks, I'll certainly try that.

As for the general idea of hacking WP's core files, I've been doing that a bit already. I try to keep track for future updates, but its living dangerously, for sure :) Sometimes there doesn't seem to be other options.

This particular issue, though, has really had me scratching my head. I understand the logic behind your suggestion (I'm not a php expert), so I'll see what I can do with it - and update back here on the results.

ergophobe

9:08 pm on Apr 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The key is making sure that the formats match (i.e. whether or not $url includes the domain; REQUEST_URI does *not*). Otherwise, it should work.

It would be nicer to just style the <li> like you suggest in your first post, but I'm not sure where that's coming from to be honest.