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/blog/2022/02/page/2/
.
What condition does this?
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?
Share Improve this question asked Mar 5, 2022 at 10:56 YcoYco 51 bronze badge1 Answer
Reset to default 0I 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"';