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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?