php - Wordpress Ajax 用于搜索结果 - 查询仅返回 1 个结果

标签 php jquery ajax wordpress while-loop

这是我的问题,我一直在尝试创建 Ajax 调用来获取搜索结果,但我的问题是我只能检索 json 中的 1 个结果。我的循环存在一些问题,到目前为止我还无法理解。

这是我的 HTML,其中应从 ajax 操作加载结果。

<div id="project-grid">
</div>

我调用 jQuery

$(document).ready(function($) {
     search_things();
});

function search_things(){
    var searchformdata = new FormData();
    searchformdata.append('action','search_results');
    $.ajax({
        method: 'post',
        url: ajaxurl,
        dataType: 'json',
        data: searchformdata,
        processData: false,
        contentType: false,
        beforeSend:function(data){
            //alert(searchformdata);
            //console.log(searchformdata);
            $('#project-grid').html('Loading...');
        },
        success:function(data) {
            $('#project-grid').append(data.projectsresults);
            //console.log(data.projectsresults);
            //alert(data.projects);
        },
        error: function(data){
            //console.log(data);
        }
    });
    //alert("a");
}

现在,我当然已经在我的 functions.php 中创建了这个小函数

add_action('wp_ajax_nopriv_search_results', 'search_results');
add_action('wp_ajax_search_results', 'search_results');
function search_results(){
    $returnprojects = return_query_projects();
    $response = array('projectsresults' => $returnprojects);
    wp_send_json( $response );
}

它调用return_query_projects()(存储我的循环的位置),如下所示:

function return_query_projects(){
        global $wp_query, $query_string;
        $query_vars = $wp_query->query_vars;
        $post_per_page = 10;
        $closed = array(
                'key' => 'closed',
                'value' => "0",
                'compare' => '='
        );
        $args = array(
            'post_type' => 'project',
            'order' => 'DESC', 
            'orderby' => 'date', 
            'posts_per_page' => $post_per_page,
            'paged' => 1,
            'meta_query' => array(
                'relation' => 'AND',
                $closed,
            ),
            'post_status' => array('publish'),
        );
        $html = array();
        query_posts($args);
        if ( have_posts() ): 
            while ( have_posts() ) : the_post();
                $html = get_post_main_function();
            endwhile;
        else:
            $html = '<div style="color:#fff; margin-top:15px">' . __('No projects posted here yet.',"Dev") . '</div>';
        endif;
        return $html;
    }

正如你所看到的,我在另一个盒子中创建了一种盒子,希望能够灵活运用。所以我的最后一个函数 get_post_main_function(); 是这样的:

function get_post_main_function()
{ 
  global $post, $current_user;
  wp_get_current_user();
  $pid = get_the_id();
  //Some other variables

  $html = array();
  $html = '<div class="card project" id="post-' . $pid . '">';

  //other code for structure

  $html = '</div>';
  return $html;
}

此代码仅返回1 个帖子,但我知道还有很多其他帖子。出了什么问题?我想后循环中有一些东西,但我不知道如何修复它。你能给我一些指示吗?谢谢。

最佳答案

使用 $html.push() 代替添加 $html 作为字符串

尝试用这个 $html.push(get_post_main_function()) 代替 $html=get_post_main_function()

关于php - Wordpress Ajax 用于搜索结果 - 查询仅返回 1 个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41304921/

相关文章:

javascript - 由 foreach Knockout.js 构建的表内的动态复选框 - 将依赖项设置为启用

javascript - jQuery 选择器性能低下

javascript - 如何在 JavaScript 中获取 .js 脚本的源代码?

PHP nextLine 与字符串

php - 使用 Laravel 4.1 和 mysql 的一个 Fortrabbit 应用程序的多个数据库

javascript - 如何通过 jQuery post() 提交单选按钮值

javascript - ParseJSON 对我的 json 数据进行排序

jquery - 将 jQuery 方法绑定(bind)到通过 Ajax 调用加载的 Html

php - 数据库中的引号被回显为 Chrome 中的菱形问号

php - 从 Google App Engine for PHP 加载远程 XML