使用ajax获取多个复选框数据wordpress(一次仅显示一个类别)

时间:2018-11-14 05:07:39

标签: javascript php wordpress

我需要您关于多个复选框的专业知识,现在即使您在复选框上选择多个类别,它也只显示一个类别,请检查以下代码,并在该项目和自定义帖子类型上使用wordpress系统,它可以工作,但是它只显示一个类别,您可以查看下面的代码

索引以是否为复选框打印术语类别

<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
            <?php
                if( $terms = get_terms( 'categories_printi', 'orderby=name' ) ) : // to make it simple I use default categories
                    echo '<div  id="chkveg" name="categories_printi"';
                    foreach ( $terms as $term ) :
                        echo '<label><input type="checkbox" name="categories_printi" value="' . $term->term_id . '">' . $term->name . '</label>'; // ID of the category as the value of an option
                    endforeach;
                    echo '</div>';
                endif;
            ?>

            <select name="date" id="misha_order_by">
                    <option name="date" value="DESC">Date &darr;</option><!-- I will explode these values by "-" symbol later -->
                    <option name="date" value="ASC">Date &uarr;</option>
                    </select>


            <button>Apply filter</button>
            <input type="hidden" name="action" value="myfilter">
        </form>
        <div id="response"></div>

Function.php,请包括content.php,这样它将显示帖子项

wp_localize_script( 'misha_scripts', 'misha_loadmore_params', array(
  'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php', // WordPress AJAX
  'posts' => json_encode( $wp_query->query_vars ), // everything about your loop is here
  'current_page' => $wp_query->query_vars['paged'] ? $wp_query->query_vars['paged'] : 1,
  'max_page' => $wp_query->max_num_pages
) );
add_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );
function misha_filter_function(){
    $args = array(
    'post_type' => 'printi',
        'orderby' => 'date', // we will sort posts by date
        'order' => $_POST['date'] // ASC or DESC
    );

    // for taxonomies / categories
    if( isset( $_POST['categories_printi'] ) )
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'categories_printi',
                'field' => 'id',
                'terms' => $_POST['categories_printi']
            )
        );

    // create $args['meta_query'] array if one of the following fields is filled
    if( isset( $_POST['price_min'] ) && $_POST['price_min'] || isset( $_POST['price_max'] ) && $_POST['price_max'] || isset( $_POST['featured_image'] ) && $_POST['featured_image'] == 'on' )
        $args['meta_query'] = array( 'relation'=>'AND' ); // AND means that all conditions of meta_query should be true

    // if both minimum price and maximum price are specified we will use BETWEEN comparison
    if( isset( $_POST['price_min'] ) && $_POST['price_min'] && isset( $_POST['price_max'] ) && $_POST['price_max'] ) {
        $args['meta_query'][] = array(
            'key' => '_price',
            'value' => array( $_POST['price_min'], $_POST['price_max'] ),
            'type' => 'numeric',
            'compare' => 'between'
        );
    } else {
        // if only min price is set
        if( isset( $_POST['price_min'] ) && $_POST['price_min'] )
            $args['meta_query'][] = array(
                'key' => '_price',
                'value' => $_POST['price_min'],
                'type' => 'numeric',
                'compare' => '>'
            );

        // if only max price is set
        if( isset( $_POST['price_max'] ) && $_POST['price_max'] )
            $args['meta_query'][] = array(
                'key' => '_price',
                'value' => $_POST['price_max'],
                'type' => 'numeric',
                'compare' => '<'
            );
    }


    // if post thumbnail is set
    if( isset( $_POST['featured_image'] ) && $_POST['featured_image'] == 'on' )
        $args['meta_query'][] = array(
            'key' => '_thumbnail_id',
            'compare' => 'EXISTS'
        );

    $query = new WP_Query( $args );

    if( $query->have_posts() ) :
        while( $query->have_posts() ): $query->the_post();
    get_template_part( 'template-parts/content', get_post_format() );
        endwhile;
        wp_reset_postdata();
    else :
        echo 'No posts found';
    endif;

    die();
}


add_action('wp_ajax_myfilter', 'misha_filter_function');
add_action('wp_ajax_nopriv_myfilter', 'misha_filter_function');

链接:用于粘贴

Function https://www.pastiebin.com/5bebabfbe13bb
index https://www.pastiebin.com/5bebac4218629
Content https://www.pastiebin.com/5bebac5cb163e

0 个答案:

没有答案