2017-07-10 37 views
1

我使用wp_get_archives与我的自定义后类型如下: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)去年

任何意见,将不胜感激!

回答

1

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

过滤 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; 

} 
+0

谢谢你的输入。在你的例子中有一个固定的日期范围,尽管日期范围“去年”每年都在变化。我怎样才能将这个动态日期应用于你的代码? – user3615851

+0

嗨,我已经更新了代码,它会在当前年份和下一年之间进行搜索。 –