【问题标题】:Add active class to custom post type date archive将活动课程添加到自定义帖子类型日期存档
【发布时间】:2021-02-09 21:30:04
【问题描述】:

我正在使用插件按年份创建自定义帖子类型日期档案:

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

我还为永久链接生成了自定义格式,以便在单击年度存档时锚定到正确的部分:

//
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);

不幸的是,当查看存档页面时,自定义结构“锚”不会将“活动”类添加到永久链接结构中,我无法实施解决方案或实验来实现这一点。任何帮助将不胜感激。

【问题讨论】:

    标签: php wordpress filtering archive


    【解决方案1】:

    您可能需要自己在这个自定义函数中添加它:

    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);
    

    【讨论】:

    • 由于某种原因,这会返回最后一个帖子的永久链接 url,而不是当前活动的存档 url,这很奇怪。
    • 更新了我的答案,以不同的方式获取当前网址。让我知道这是否有帮助。
    猜你喜欢
    • 2014-06-16
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 2013-08-04
    • 1970-01-01
    相关资源
    最近更新 更多