我是wordpress的新手,正在尝试添加Year and Month过滤器。我已经通过类别页面上的DIVI代码块添加了HTML内容
<select name="year_filter" id="ytr">
<option value="">Select Year</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
</select>
<select name="date_filter" id="dtr">
<option value="">Select Month</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<script>
jQuery(document).ready(function( $ ){
$('#dtr').change(function() {
window.location.href = "/bulletin/?month_filter="+$(this).val();
});
$('#ytr').change(function() {
window.location.href = "/bulletin/?year_filter="+$(this).val();
});
});
</script>
到目前为止,到目前为止,这是合乎逻辑的
function date_filter_where($where = '') {
if( isset($_GET['year_filter']) && $_GET['year_filter'] != '')
{
$filtered_date_filter_from = sanitize_text_field( $_GET['year_filter'].'-01-01', false );
$filtered_date_filter_to = sanitize_text_field( $_GET['year_filter'].'-12-31', false );
$where .= " AND post_date >= '" . date('Y-m-d', strtotime($filtered_date_filter_from)). " 00:00:00' AND post_date <= '" . date('Y-m-d', strtotime($filtered_date_filter_to)) . " 23:59:59'";
}
if( isset($_GET['month_filter']) && $_GET['month_filter'] != '')
{
$filtered_date_filter_from = sanitize_text_field( date('Y').'-'.$_GET['month_filter'].'-01', false );
$filtered_date_filter_to = sanitize_text_field( date('Y').'-'.$_GET['month_filter'].'-31', false );
$where .= " AND post_date >= '" . date('Y-m-d', strtotime($filtered_date_filter_from)). " 00:00:00' AND post_date <= '" . date('Y-m-d', strtotime($filtered_date_filter_to)) . " 23:59:59'";
}
return $where;
}
我不确定上面的Php代码是否有效。如果正确,我应该在哪里添加上述功能以应用名为公告的特定类别页面