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.
这是我的问题,我一直在尝试创建一个Ajax调用来获取搜索结果,但我的问题是,我只能检索json中的一个结果。我的循环有一些问题,到目前为止我还不能理解。
Here is my HTML, where the results should be loaded from the ajax action.
这是我的HTML,结果应该从ajax操作中加载。
<div id="project-grid">
</div>
I call the jQuery
我叫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
当然,我在函数中创建了这个函数。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:
它调用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;
}
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:
正如您所看到的,我在另一个盒子中创建了一种盒子,希望它能灵活使用。最后一个函数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;
}
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.
这个代码只返回一个post,但是我知道还有很多其他的。什么错了吗?我想在post循环中有一些东西,但是我不知道如何修复它。你能给我指路吗?谢谢。
1 个解决方案
#1
3
Use $html.push()
in place of adding $html
as string
使用$html.push()代替添加$html作为字符串
Try this $html.push(get_post_main_function())
in place of $html=get_post_main_function()
尝试使用$html.push(get_post_main_function()代替$html=get_post_main_function()
#1
3
Use $html.push()
in place of adding $html
as string
使用$html.push()代替添加$html作为字符串
Try this $html.push(get_post_main_function())
in place of $html=get_post_main_function()
尝试使用$html.push(get_post_main_function()代替$html=get_post_main_function()