我可以在Ajax发送过滤后的数据返回到PHP文件,并将其网页上写一遍?(Can I send fil

2019-11-05 05:23发布

我实现了我的自定义文章类型和内容是我在这样while循环页面上列出:

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

        $loop = new WP_Query($args);

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

        <div class="col-md-6">
         // data are listed here
        </div>
          <?php endwhile;
        endif;
        ?>

和我提交我尝试根据一些自定义分类过滤数据:

$ = jQuery;

var search = $("#search-studien");

var searchForm = search.find("studien");

$(document).ready(function () {

    $('#studien').submit(function (evt) {
        event.preventDefault();

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


        var html;

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

                if(response)
                {
                    // probably here I need to send filtered data back to PHP file and write them again
                }

            }
        });
    })
});

我使用自定义简码和回调()函数:

function search_callback()
{
    header('Content-Type: application/json;charset=utf-8');

        $type = "";


        $type = $_GET['type'];


    $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);

       // echo json_encode($search_query);
    }


    else{
        $search_query = new WP_Query( $args );
    }

    if ( $search_query->have_posts() ) {

        $result = array();

        while ($search_query->have_posts()) {

            $search_query->the_post();

            $result[] = array(
                "id" => get_the_ID(),
                "title" => get_the_title(),
                "permalink" => get_permalink(),


            );
        };


        wp_reset_query();

        echo json_encode($search_query);
    }

    else {
        // nothing
    }



    wp_die(); global $argsAjaxFilter;

    $argsAjaxFilter = $search_query;


}

正如你所看到的,$ SEARCH_QUERY代表我的过滤后的数据。 这种方法是根据教程,但我不能老是用响应阵列()...和最佳办法,我就是以某种方式发送$ SEARCH_QUERY到PHP文件,在那里我可以再写我的新的自定义类型后的数据。 请,有人给我建议? 那是好建议?

Answer 1:

所以,主要的问题是要能够呈现的HTML发送到浏览器。 为此,我们将需要一个模板,这样我们就可以在不同的地方重复使用。

// your-theme/studien.php
<div class="studien-wrapper">
<?php
  if($wp_query->have_posts()):
    while($wp_query->have_posts()) : $wp_query->the_post();
      // Do the thing
    endwhile;
  endif;
?>
</div>

接下来,我们需要的是在需要时加载它。 我们可以通过做get_template_part 。 此功能会自动通过一些全局变量(如$wp_query )的模板,你可以看看源代码 ,以了解更多信息。 当在“AJAX模式”下使用它,我们需要捕获输出,不是发送到浏览器(尽管我们可以),所以我们使用输出缓冲。 对于“正常的网页”只是忽略这些调用。

// Set up query args
global $wp_query = new WP_Query($args);
ob_start();  // Capture the output.
get_template_part('studien');
$html_result = ob_get_clean(); 

而现在是构成响应的问题:

$result['html'] = $html_result;
if (!is_array($type)) {
  $type = array($type);
}
$result['query_terms'] = $type;

$json = wp_json_encode($result);
echo $json;

wp_die();

您还可以跟踪在客户端的搜索词,避免最后一部分。 取决于你所需要的东西。

要更新,你可以做你的Ajax调用以下页面:

$.ajax({
    url: ajaxurl,
    data: data,
    success: function (response) {
        $('.studien-wrapper').replaceWith(response.html);
        // Update search terms, etc
    }
});


文章来源: Can I send filtered data with Ajax back to PHP file and write them again on page?