【问题标题】:wp_get_archives filter by range: this month, last month / this year, last yearwp_get_archives 按范围过滤:本月、上月/今年、去年
【发布时间】:2017-12-13 23:08:59
【问题描述】:

我在自定义帖子类型中使用 wp_get_archives,如下所示:

  <?php $args = array(
        'post_type'    => 'donation',
        'type'         => 'yearly',
        'limit'        => '1',
        'echo'         => 0
    );
    echo '<ul>'.wp_get_archives($args).'</ul>'; ?>

有没有办法按相对日期范围过滤: 1)本周 2) 上周 3) 今年 4) 去年

任何建议将不胜感激!

【问题讨论】:

    标签: php wordpress custom-post-type archive


    【解决方案1】:

    有办法做到这一点。您可以使用下面的过滤器来实现它,添加。

    过滤器 getarchives_where

    参考法典: https://developer.wordpress.org/reference/hooks/getarchives_where/

    示例代码:

    add_filter('getarchives_where', 'customFunction');
    function customFunction($where, $args){
        //Add any specific post type check if required
        $startDate = date('Y');
        $endDate = date('Y', strtotime('+1 year'));
            $where.= ' AND `post_date` BETWEEN ' . $startDate . ' AND '.$endDate.'';
    
        return $where;
    
    }
    

    【讨论】:

    • 感谢您的意见。在您的示例中,有一个固定的日期范围,尽管“去年”的日期范围每年都在变化。如何将此动态日期应用于您的代码?
    • 您好,我更新了代码,它将在今年和明年之间进行搜索。
    猜你喜欢
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 2014-02-12
    • 1970-01-01
    • 2021-05-11
    • 2014-08-20
    相关资源
    最近更新 更多