Active class with custom wp_get_archives HTML output

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 = \tli$beforea 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?

Topic wp-get-archives Wordpress

Category Web


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"';

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.