小工具应按年显示帖子档案,并按月单击

时间:2016-03-31 作者:MrKainig

我想显示如图所示的后期存档。按年份显示,单击它可以打开月份。我不知道如何实现这一点,您是否可以自定义标准WordPress存档小部件?

enter image description here

2 个回复
SO网友:Ciprian

我为一位客户做了这件事,看起来是这样的:

PHP代码:

<dl class="tree-accordion">
    <?php
    $currentyear = date("Y");
    $years = range($currentyear, 1950);
    foreach($years as $year) { ?>
        <dt><a href=""><i class="fa fa-fw fa-plus-square-o" aria-hidden="true"></i> <?php echo $year; ?></a></dt>
        <?php
        $archi = wp_get_archives(\'echo=0&show_post_count=1&type=monthly&year=\' . $year);
        $archi = explode(\'</li>\', $archi);
        $links = array();
        foreach($archi as $link) {
            $link = str_replace(array(\'<li>\', "\\n", "\\t", "\\s"), \'\' , $link);
            if(\'\' != $link)
                $links[] = $link;
            else
                continue;
        }
        $fliplinks = array_reverse($links);
        if(!empty($fliplinks)) {
            echo \'<dd>\';
            foreach($fliplinks as $link) {
                echo \'<span>\' . $link . \'</span>\';
            }
            echo \'</dd>\';
        } else {
            echo \'<dd class="tree-accordion-empty"></dd>\';
        }
        ?>
    <?php } ?>
</dl>
存档覆盖筛选器:

/*
 * Add filter to query archives by year
 */
function newmarket_getarchives_filter($where, $args) {
    if(isset($args[\'year\'])) {
        $where .= \' AND YEAR(post_date) = \' . intval($args[\'year\']);
    }

    return $where;
}

add_filter(\'getarchives_where\', \'newmarket_getarchives_filter\', 10, 2);
CSS代码:

.tree-accordion {
    line-height: 1.5;
}
.tree-accordion dt, .tree-accordion dd {}
.tree-accordion dt a {
    display: block;
}
.tree-accordion .fa {
    color: #666666;
}
.tree-accordion dd a {}
.tree-accordion dd span {
    display: block;
}
.tree-accordion dd {
    margin: 0 0 0 20px;
}
Javascript代码:

jQuery(document).ready(function(){
    var allPanels = jQuery(\'.tree-accordion > dd\').hide();

    jQuery(\'.tree-accordion > dt > a\').click(function() {
        $target = jQuery(this).parent().next();

        if(!$target.hasClass(\'active\')) {
            allPanels.removeClass(\'active\').slideUp();
            $target.addClass(\'active\').slideDown(100);
        }

        return false;
    });

    jQuery(\'.tree-accordion-empty\').prev().hide();
});
UPDATE: 逐年逐月删除:

更改此行:

$link = str_replace(array(\'<li>\', "\\n", "\\t", "\\s"), \'\' , $link);
收件人:

$link = str_replace(array(\'<li>\', "\\n", "\\t", "\\s"), \'\', $link);
$link = str_replace($year, \'\', $link);
最终结果:

enter image description here

SO网友:Isaac Lubow

“归档”小部件利用wp_get_archives() 给出正确的参数(特别是format 参数),可以提供所需的标记。我想说定制这个小部件是一个很好的开始。

相关推荐

How to disable date archives?

有没有办法在WordPress中禁用日期存档?例如,我根本不想有日期档案(例如,example.com/2021/06/11)。有没有办法关闭这些档案?我在Google上找到了以下代码片段,但它似乎没有改变任何东西。 add_action( \'template_redirect\', \'remove_date_archives\' ); function remove_date_archives() { // Check if we are on date a