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 VMware安装虚拟网络驱动程序不成功,如何解决?(操作系统-linux)
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥15 QFILHelper怎么恢复全字库,提示进程已完成,只能恢复分区文件