duanlanqian9974 2016-12-23 16:36
浏览 65
已采纳

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

Here is my problem, I've been trying to create a Ajax call to get my search results but my problem is that I can only retrieve 1 result in json. There is some problem with my loop which I can't understand so far.

Here is my HTML, where the results should be loaded from the ajax action.

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

I call the 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");
}

Now, of course I've created this little fuction in my 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 );
}

Which calls the return_query_projects() (where my loop is stored) looking like:

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;
    }

As you can see, I've created a kind of box in another box with the hope to take it flexible. So my last function get_post_main_function(); is something like this:

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;
}

This code is returning 1 only post, but I know there are many others. What's going wrong? I suppose there is something in the post loop but I don't know how to fix it. Can you give me some directions? Thanks.

  • 写回答

1条回答 默认 最新

  • doulu8847 2016-12-23 16:54
    关注

    Use $html.push() in place of adding $html as string

    Try this $html.push(get_post_main_function()) in place of $html=get_post_main_function()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有没有人会打学生成绩管理系统呀
  • ¥15 在使用Fiddler和夜神模拟器抓包的时候一直出现443该怎么办啊QAQ搜了好几个笔记都没有解决
  • ¥15 3x7的二维数组A、B、C,A中的任意1个数组元素与B的任意1个数组元素、同时又与C的任意1个数组元素比较,把不同位置出现相同数的比较称为无意义,反之称为有意义,把有意义的比较打印输出。
  • ¥20 预测模型怎么处理原始数据(随机森林)
  • ¥20 请问discuz3.5如何实现插入ckplayer全能播放器功能呢?
  • ¥15 thingsboard代码编译出错误
  • ¥15 博途v18仿真报错怎么解决
  • ¥15 欧姆龙plc枕式包装机 ST编程
  • ¥15 为啥快手广告联盟的广告这么难出来
  • ¥15 k8s集群重启后,kubelet一直报systemctl restart kubelet.service "Failed to delete cgroup paths"