使用AJAX加载WordPress搜索结果

我正在尝试使用AJAX在同一页面上加载我的WordPress搜索结果。 但我无法弄清楚如何在页面上显示搜索结果。

我用Google搜索并发现了很多代码,但大多数代码都不适用于我。 我最接近的就是这个。 这是我到目前为止所得到的。

HTML

jQuery (阻止搜索转到其他页面并加载AJAX文件)

  $(function() { $("#lets_search").bind('submit',function() { var value = $('#str').val(); $.post('wp-content/themes/MyTheme/includes/search-post-results.php',{value:value}, function(data){ $("#search_results").html(data); }); return false; }); }); 

搜索后results.php

这就是我被困住的地方。 如果我加载正常loop我会得到500内部服务器错误。 现在我得到了这个:

   found_posts ); echo get_search_query(); ?> 

我想我必须包含WordPress核心文件,所以我可以使用WordPress代码,但echo get_search_query()什么都不做? $_POST['value']; 输出搜索的输入。

你的代码有效。 我把它写成了一个插件。 没有你的代码,我无法加载AJAX。 我唯一改变的是评论以下因为它与其他插件冲突并且抛出错误,但它没有它就能很好地工作(加载wp-config.php是必需的)。

  // include_once $path . '/wp-load.php'; // include_once $path . '/wp-includes/wp-db.php'; // include_once $path . '/wp-includes/pluggable.php'; 

非常感谢您为我们更新您的工作代码。

(代表问题作者发布解决方案)

我已使用以下代码解决了我的问题:

HTML

 

jQuery (阻止搜索转到其他页面并加载AJAX文件)

  $(function() { $("#lets_search").bind('submit',function() { var value = $('#str').val(); $.post('wp-content/themes/MyTheme/includes/search-post-results.php',{value:value}, function(data){ $("#search_results").html(data); }); return false; }); }); 

搜索后results.php

     'team', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => -1, 's' =>$s ); // The Query $the_query = new WP_Query($args); if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); $value = get_the_id(); $mail = get_field('e-mail_adres', $value); $afd = get_field('functie', $value); $intrn = get_field('intern_telefoonnummer', $value); $mob = get_field('mobiel', $value); $locatie = get_field('locatie', $value); ?> 

Tel. Intern:
Tel. Intern (extra):
E-mail adres:
Mobiel werk:
Mobiel privé:
Locatie:

Geen collega's gevonden

Probeer het opnieuw