WordPress.org

Ready to get started?Download WordPress

Forums

wp_get_archives highlight current archive please! (12 posts)

  1. engelle
    Member
    Posted 2 years ago #

    i've been trying to find a solution to highlight the current archive and the closest i've found is this:

    http://wordpress.org/support/topic/modify-wp_get_archives-highlight-current-archive-category?replies=11

    but i can't seem to get it to work for me. any updates or things i might be missing?

  2. h0tw1r3
    Member
    Posted 2 years ago #

    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');
  3. engelle
    Member
    Posted 2 years ago #

    thanks, but no go.

  4. h0tw1r3
    Member
    Posted 2 years ago #

    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.

  5. engelle
    Member
    Posted 2 years ago #

    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.

  6. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    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%.

  7. dioz
    Member
    Posted 2 years ago #

    the solution posted by h0tw1r3 works like a charm.
    thanks you!

  8. nikloveland
    Member
    Posted 2 years ago #

    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.'/' ) );
  9. ted_the_bad
    Member
    Posted 1 year ago #

    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');
  10. J Grandin
    Member
    Posted 1 year ago #

    Thanks ted_the_bad!
    It was exactly what I looking for. Very helpful. Thanks also to h0tw1r3 for the first version of course.

  11. crdunst
    Member
    Posted 1 year ago #

    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!

  12. Vitaliy K
    Member
    Posted 1 year ago #

    Thanks. cool filter

Topic Closed

This topic has been closed to new replies.

About this Topic