如何使用Ajax将过滤的数据发送回PHP文件?

时间:2018-09-20 21:27:18

标签: javascript php ajax wordpress

我实现了“自定义帖子类型”,并且我需要根据某些分类法过滤数据。

具有自定义帖子类型数据的PHP代码:

$args = array('post_type' => 'studien', 'order' => 'ASC', 'posts_per_page' =>  intval($allItemsCount));



    $loop = new WP_Query($args);

    if($loop->have_posts()):
    while($loop->have_posts()) : $loop->the_post();?>

    <div class="col-md-6">
        <div class="cpt-studies-block">
            <div class="row">
                <div class="col-md-6">
                    <a class="zoom-picture-hover" href="<?php the_permalink()?>">
                        <div class="cpt-studies-block-image picture">
                            <?php $thumb_img=wp_get_attachment_image_src(get_field('image'),'custom-crop-studien'); ?>
                            <img class="img" src="<?php echo $thumb_img[0]; ?>">
                        </div>
                    </a>
                </div>
                <div class="col-md-6">
                        <span><?php the_field('date')?></span>
                        <h3>
                            <a href="<?php the_permalink()?>">
                                <?php the_title()?>
                            </a>
                        </h3>
                        <p class="cpt-studies-block-text"><?php the_field('text')?></p>
                        <a href="<?php the_permalink()?>" class="cpt-studies-block-link link-read-more"> <?php
                            $svg_file = 'wp-content/themes/bb_boilerplate/img/svg/icon_arrow.svg';
                            $svg_file = file_get_contents($svg_file);
                            $find_string   = '<svg';
                            $position = strpos($svg_file, $find_string);
                            $svg_file_new = substr($svg_file, $position);
                            echo $svg_file;
                            ?>
                            <?php if (get_field('button_text')):
                                the_field('button_text');
                            else : echo __('More', 'dw');?>
                            <?php endif;?>
                        </a>
                </div>
            </div>
        </div>
    </div>

    <?php endwhile;
    endif;
    ?>

PHP函数处理我的分类标准值...

$type = $_GET['type'];

$result = array();

$args = array(
        "post_type" => "studien",
        "post_per_page" => -1,
        "relation" => "AND"
);


if($type != "")
    $args['tax_query'][] = array(

        'taxonomy' => 'market',
        'field' => 'slug',
        'terms' => $type
    );

$search_query = new WP_Query($args);

while($search_query->have_posts()) {

    $search_query->the_post();

    $result[] = array(
      "id" => get_the_ID(),
      "title" => get_the_title(),
      "permalink" => get_permalink(),
      "date" => get_field('date'),
      "text" => get_field('text'),
      "button_text" => get_field('button_text'),
      "button_link" => get_field('button_link'),
      "image" => get_field('image'),
      "imageSvg" => $svg_file
    );

  echo json_encode($result);
}

有关使用自定义分类法过滤数据的Java代码:

jQuery(function($) { // Short for jQuery(document).ready(function() {
    $(document).on('submit', searchForm, function (event) {

       event.preventDefault();

       var data = {
           action: "studien_search",
           type: $("#taxonomy-market-type").val()
       };



        $.ajax({
            url : ajaxurl,
            data: data,
            success: function (response) {

            // here send data back to PHP code above

} 
})


    });
});

过滤工作得很好,但是该版本进行处理并以JSON编码响应,因此我需要在$search_query部分的AJAX函数中发送success()。有谁知道这是怎么做到的吗 ???

0 个答案:

没有答案