【问题标题】:Filter wordpress query by checkboxes通过复选框过滤 wordpress 查询
【发布时间】:2012-09-08 03:13:18
【问题描述】:

我有一种情况,我有两种不同的帖子类型的查询,我必须在顶部选择带有帖子类型名称的复选框。默认情况下,我不会选中这些复选框,并且如果未选中某个主题以从查询中删除该帖子类型和附属于该帖子类型的帖子。

【问题讨论】:

    标签: wordpress filter


    【解决方案1】:

    这部分是间接的,但你明白了。将值分配给已检查/​​未检查的值。

    if($checkboxone == '1') { 
        $types = array( 'typeone' )
    }
    if($checkboxtwo == '1') {
        $types = array( 'typetwo' )
    }
    if($checkboxtwo == '1' && $checkboxone == '1'){
        $types = array( 'typeone', 'typetwo' )
    }
    

    然后通过类似这样的方式将该值插入到您的 WP_Query 中。它的文档是here

    // The Query
    $the_query = new WP_Query( array( 'post_type' => $types );
    
    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
        //DO STUFF
    endwhile;
    
    // Reset Post Data
    wp_reset_postdata();
    

    【讨论】:

    • 嗨,Alex 首先感谢您的快速响应。我仍然无法确定解决方案以查看哪个被检查,我尝试了许多解决方案,但它们似乎都不起作用:(你知道有什么好的解决方案吗?这里有一些主题:
    • 解决方案 1 php: foreach ($_POST['box'] as $id=>$checked){ if ($checked =='on') }
    • 解决方案 2 php: foreach ($_POST['box'] as $id){ //处理你的id }
    • 解决方案 3 PHP: if ($_POST['check_box_1']=='1') { /*为勾选做某事*/ } else { /*为未勾选做某事*/ }
    • 解决方案 4 Php: // $_POST['rows'] 包含所有选中复选框的值,例如: // array('uniqueIdForThisRow', 'anotherId', ...) foreach ($_POST['rows'] as $ row) { if ($row == 'uniqueIdForThisRow') { // 做点什么 } }
    猜你喜欢
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    相关资源
    最近更新 更多