wp_get_archives筛选范围:本月、上月/今年、去年
wp_get_archives filter by range: this month, last month / this year, last year
我按如下方式使用 wp_get_archives 和我的自定义 post 类型:
<?php $args = array(
'post_type' => 'donation',
'type' => 'yearly',
'limit' => '1',
'echo' => 0
);
echo '<ul>'.wp_get_archives($args).'</ul>'; ?>
有没有办法按相对日期范围过滤:
1)本周
2) 上周
3)今年
4) 去年
如有任何建议,我们将不胜感激!
有办法做到。您可以使用下面的过滤器来实现它,添加 .
过滤器 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;
}
我按如下方式使用 wp_get_archives 和我的自定义 post 类型:
<?php $args = array(
'post_type' => 'donation',
'type' => 'yearly',
'limit' => '1',
'echo' => 0
);
echo '<ul>'.wp_get_archives($args).'</ul>'; ?>
有没有办法按相对日期范围过滤: 1)本周 2) 上周 3)今年 4) 去年
如有任何建议,我们将不胜感激!
有办法做到。您可以使用下面的过滤器来实现它,添加 .
过滤器 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;
}