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 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧