Created
November 5, 2012 13:46
-
-
Save enigmaticape/4017258 to your computer and use it in GitHub Desktop.
Wordpress PHP function which adds CSS class to currently selected archive list item
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://wordpress.org/support/topic/wp_get_archives-highlight-current-archive-please | |
/* Add a class to the archive link if it is the current one | |
so we can, e.g. highlight it */ | |
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 ) ) { | |
$link_html = preg_replace( '/(<[^\s>]+)/','\1 class="current-arch"',$link_html,1 ); | |
} | |
return $link_html; | |
} | |
/* and of course you need to add the following hook */ | |
add_filter('get_archives_link', 'theme_get_archives_link'); |
It is linked from a post at Enigmatic Ape blog on creating a Wordpress function to add similar functionality to tags, you can find it at http://www.enigmaticape.com/blog/wordpress-listing-tags-like-wp_list_categories/
Thanks for the code, it helped me a lot.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NB this code is sourced from the referenced website, I am not the author. It is placed here for display purposes.