简体   繁体   English

添加活动 class 到自定义帖子类型日期存档

[英]Add active class to custom post type date archive

I'm using a plugin to create custom post type date archives by year:我正在使用插件按年创建自定义帖子类型日期档案:

<?php 
    $args = array(
        'post_type' => 'unistused',
        'type' => 'yearly',
        'format' => 'anchor',
        );
    cptda_get_archives($args); 
?>

I've also generated a custom format for the permalink so it would anchor to the correct section when clicking the yearly archive:我还为永久链接生成了自定义格式,因此在单击年度存档时它会锚定到正确的部分:

//
add_filter ('get_archives_link',
function ($link_html, $url, $text, $format, $before, $after) {
    if ('anchor' == $format) {
        $link_html = "<li class='year-archive'><a href='$url#dreams'>"
                   . "$text"
                   . '</a></li>';
    }
    return $link_html;
}, 10, 6);

Unfortunately the custom structure 'anchor' does not add the 'active' class to the permalink structure when a archive page is viewed and I'm unable to implement a solution or experiment to achieve this.不幸的是,当查看存档页面时,自定义结构“锚点”不会将“活动”class 添加到永久链接结构中,我无法实施解决方案或实验来实现这一点。 Any help would be appreciated.任何帮助,将不胜感激。

You probably need to add it yourself in this custom function:您可能需要在这个自定义 function 中自己添加它:

add_filter ('get_archives_link',
function ($link_html, $url, $text, $format, $before, $after) {
    if ('anchor' == $format) {
        
        global $wp;
        $current_url = home_url( add_query_arg( array(), $wp->request ) );

        $active_classname =  $current_url === $url ? 'active' : '';

        $link_html = "<li class=\"year-archive\"><a href=\"$url#dreams\" class=\"$active_classname\">$text</a></li>";
    }
    return $link_html;
}, 10, 6);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM