0

I use a filter on get_archives_link for the HTML output and I try to reproduce the default aria-current="page" attribute on the <a> tag when we are on the archive page.

if ( ! function_exists( 'o2_archives_html' ) ) :
    function o2_archives_html( $link_html, $url, $text, $format, $before, $after ) {

        if ( 'html' === $format )
        $link_html = "\t<li>$before<a href='$url'><span class='label'><span class='title'>$text</span></span></a>$after</li>\n";

        return $link_html;

    }
endif; // o2_archives_html()
add_filter( 'get_archives_link', 'o2_archives_html', 10, 6 );

I would like to put an "active" class on the <li class="active"> tag when we are for example on February 2022 site.com/blog/2022/02/page/2/.

What condition does this?

1 Answer 1

0

I haven't found an easy way but here is a solution:

I get the month and year from the current archive and compare them with the text.

$year      = get_query_var( 'year' );
$monthnum  = get_query_var( 'monthnum' );
$monthname = $GLOBALS['wp_locale']->get_month( $monthnum );
$date      = $monthname . ' ' . $year;

if ( $text === $date )
$active_class = 'class="current-menu-item"';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.