我正在 WordPress 中构建一个带有过滤功能的页面。基本上我想在单击复选框时更改数据的输出。我现在拥有的:
<form class="compo-filter">
<input type="checkbox" name="compo-cat" value="indoor" onclick="if(this.checked){this.form.submit();}" />Indoor
<input type="checkbox" name="compo-cat" value="outdoor" onclick="if(this.checked){this.form.submit();}" />Outdoor
<input type="checkbox" name="compo-cat" value="men" onclick="if(this.checked){this.form.submit();}" />Men
<input type="checkbox" name="compo-cat" value="women" onclick="if(this.checked){this.form.submit();}" />Women
<input type="checkbox" name="order" value="order" onclick="if(this.checked){this.form.submit();}" />Show only competitions currently in progress
</form>
<?php
$today = date('Y-m-d');
$args = array(
'post_type' => 'competitions',
'posts_per_page' => 10,
'meta_key' => 'startdate',
'meta_compare' => '>=',
'meta_value' => $today,
'orderby' => 'startdate',
'order' => 'ASC'
);
$args2 = array(
'post_type' => 'competitions',
'posts_per_page' => 10,
'meta_key' => 'startdate',
'orderby' => 'startdate',
'order' => 'ASC'
);
if ($_POST['order'] == 'order') {
$query = new WP_Query($args2);
} else {
$query = new WP_Query($args);
}
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
但是上述解决方案不起作用,我无法找出问题所在。页面重新加载,URL 显示所选值 /?order=order
。
最佳答案
我认为问题在于表单方法,您在代码中使用 $_POST
但通过输入特定表单 method = "post"
您可以得到这个使用 $_POST
方法工作。
默认情况下,如果没有 method
属性,它将在提交表单时将您带到 get
方法。
关于php - 在 WordPress 中使用复选框过滤输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20740546/