i've been trying to find a solution to highlight the current archive and the closest i've found is this:
but i can't seem to get it to work for me. any updates or things i might be missing?
i've been trying to find a solution to highlight the current archive and the closest i've found is this:
but i can't seem to get it to work for me. any updates or things i might be missing?
Try adding this to your theme's functions.php
function theme_get_archives_link ( $link_html ) {
global $wp;
static $current_url;
if ( empty( $current_url ) ) {
$current_url = add_query_arg( $_SERVER['QUERY_STRING'], '', home_url( $wp->request ) );
}
if ( stristr( $link_html, $current_url ) !== false ) {
$link_html = preg_replace( '/(<[^\s>]+)/', '\1 class="current"', $link_html, 1 );
}
return $link_html;
}
add_filter('get_archives_link', 'theme_get_archives_link');thanks, but no go.
Stock WP 3.3.1 works just fine for me. "thanks, but no go" is a bit terse for someone looking for help.
If you know how to debug, that should be plenty to get you started. Pay particular attention to the value of $current_url.
it's not meant to be terse, i'm not sure what to tell you about why it doesn't work since the code is beyond my knowledge (i can figure out the code fine in the previous link). all i can figure out is that it doesn't seem to make it into the if statement.
please post a link to your site to allow access to more details -
the suggested code for instance failed in my test with the default permalink setting, but worked ok for the default 'archive widget' - monthly archives - with permalinks set to %postname%.
the solution posted by h0tw1r3 works like a charm.
thanks you!
I had to change the 5th line to include the trailing '/' when using the code in theme pages like so:
$current_url = add_query_arg( $_SERVER['QUERY_STRING'], '', home_url( $wp->request.'/' ) );Thank you for posting this H0tw1r3 - its very helpful! The only problem I found with it is that it wasn't working if you navigate to a 2nd page in the archive. I've tweaked it a little bit so it now works on any page number. Hopefully someone might finds this useful.
function theme_get_archives_link ( $link_html ) {
global $wp;
static $current_url;
if ( empty( $current_url ) ) {
$current_url = add_query_arg( $_SERVER['QUERY_STRING'], '', home_url( $wp->request ) );
}
if ( stristr( $current_url, 'page' ) !== false ) {
$current_url = substr($current_url, 0, strrpos($current_url, 'page'));
}
if ( stristr( $link_html, $current_url ) !== false ) {
$link_html = preg_replace( '/(<[^\s>]+)/', '\1 class="current"', $link_html, 1 );
}
return $link_html;
}
add_filter('get_archives_link', 'theme_get_archives_link');Thanks ted_the_bad!
It was exactly what I looking for. Very helpful. Thanks also to h0tw1r3 for the first version of course.
I'd just like to add my thanks too - I've just spent an hour trying to do this, and trying widget plugins....then found this thread. Thanks h0tw1r3 and ted_the_bad!
Thanks. cool filter
This topic has been closed to new replies.