douwojiao5919 2013-12-23 09:28
浏览 63
已采纳

使用WordPress中的复选框过滤输出

I'm building a page in WordPress with a filter function. Basically I want to change the output of the data when you click on the checkbox. What I have now:

    <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(); ?>

The above solution does however not work, and I'm not able to work out what's going wrong. The page reloads, and the URL is showing the selected value /?order=order.

  • 写回答

3条回答 默认 最新

  • douya1855 2013-12-23 09:57
    关注

    I think the issue lies with the form method, you are using $_POST in the code but by putting the specific form method = "post" you can get this working in $_POST method.

    By default without method attribute it will take you to the get method while submitting the form.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?