最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp get archives - Active class with custom wp_get_archives HTML output

programmeradmin3浏览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/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 badge
Add a comment  | 

1 Answer 1

Reset to default 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"';
发布评论

评论列表(0)

  1. 暂无评论