Javascript 使用AJAX加载Wordpress搜索结果
我正在尝试使用AJAX在同一页面上加载我的WordPress搜索结果。但是我不知道如何在页面上显示搜索结果 我在谷歌上搜索了很多代码,但大多数都不适合我。我最接近的就是这个。这是我到目前为止得到的 HTMLJavascript 使用AJAX加载Wordpress搜索结果,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,我正在尝试使用AJAX在同一页面上加载我的WordPress搜索结果。但是我不知道如何在页面上显示搜索结果 我在谷歌上搜索了很多代码,但大多数都不适合我。我最接近的就是这个。这是我到目前为止得到的 HTML <section id="toplevel-frontpage"> <div class="container"> <div class="block"> <div class="row">
<section id="toplevel-frontpage">
<div class="container">
<div class="block">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-8 col-lg-8">
<form id="lets_search" action="" style="width:400px;margin:0 auto;text-align:left;">
Search:<input type="text" name="str" id="str">
<input type="submit" value="send" id="send">
</form>
<div id="search_results"></div>
</div>
</div>
</div>
</div>
</section>
<section id="toplevel-frontpage">
<div class="container">
<div class="block">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-8 col-lg-8">
<form id="lets_search" action="" style="width:400px;margin:0 auto;text-align:left;">
Search:<input type="text" name="str" id="str">
<input type="submit" value="send" id="send">
</form>
<div id="search_results"></div>
</div>
</div>
</div>
</div>
</section>
search post results.php
这就是我被困的地方。如果我加载一个正常的循环
,我会得到500个内部服务器错误。现在我知道了:
<?php session_start(); ?>
<?php
/*
Template Name: Search Page
*/
?>
<?php
$value = $_POST['value'];
$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-config.php';
include_once $path . '/wp-load.php';
include_once $path . '/wp-includes/wp-db.php';
include_once $path . '/wp-includes/pluggable.php';
echo sprintf( __( '%s Search Results for ', 'html5blank' ), $wp_query->found_posts ); echo get_search_query();
?>
我想我必须包含WordPress核心文件,这样我才能使用WordPress代码,但是
echo get\u search\u query()
什么都不做<代码>$\u POST['value']代码>输出搜索的输入 您的代码工作正常。我把它写进了一个插件。没有您的代码,我无法加载AJAX。我唯一改变的是注释掉以下内容,因为它与其他插件冲突,并且抛出错误,但是没有它,它工作得很好(需要加载wp-config.php)
非常感谢您为我们更新您的工作代码。(代表问题作者发布解决方案)
我已使用以下代码修复了我的问题:
HTML
<section id="toplevel-frontpage">
<div class="container">
<div class="block">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-8 col-lg-8">
<form id="lets_search" action="" style="width:400px;margin:0 auto;text-align:left;">
Search:<input type="text" name="str" id="str">
<input type="submit" value="send" id="send">
</form>
<div id="search_results"></div>
</div>
</div>
</div>
</div>
</section>
<section id="toplevel-frontpage">
<div class="container">
<div class="block">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-8 col-lg-8">
<form id="lets_search" action="" style="width:400px;margin:0 auto;text-align:left;">
Search:<input type="text" name="str" id="str">
<input type="submit" value="send" id="send">
</form>
<div id="search_results"></div>
</div>
</div>
</div>
</div>
</section>
search post results.php
<?php session_start(); ?>
<?php
/*
Template Name: Search Page
*/
?>
<?php
$value = $_POST['value'];
$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-config.php';
include_once $path . '/wp-load.php';
include_once $path . '/wp-includes/wp-db.php';
include_once $path . '/wp-includes/pluggable.php';
?>
<?php
$s=$_POST['value'];
$args = array(
'post_type' => '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);
?>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
<div style="min-height:150px;">
<div class="col-xs-12">
<h3><?php the_title(); ?></h3>
</div>
<div class="col-xs-12 col-sm-12 col-md-2 col-lg-2">
<div class="thumbnail-image">
<?php echo get_the_post_thumbnail(); ?>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-10 col-lg-10">
<div class="row">
<table class="zoek-col">
<?php if( get_field('intern_telefoonnummer') ): ?>
<tr>
<td class="td-top">Tel. Intern:</td>
<td><?php the_field('intern_telefoonnummer'); ?></td>
</tr>
<?php endif; ?>
<?php if( get_field('intern_utr') ): ?>
<tr>
<td class="td-top">Tel. Intern (extra):</td>
<td><?php the_field('intern_utr'); ?></td>
</tr>
<?php endif; ?>
<tr>
<td class="td-top">E-mail adres:</td>
<td><a href="mailto:<?php echo $mail; ?>"><?php echo $mail; ?></a></td>
</tr>
<?php if( get_field('mobiel_werk') ): ?>
<tr>
<td class="td-top">Mobiel werk:</td>
<td><?php the_field('mobiel_werk'); ?></td>
</tr>
<?php endif; ?>
<tr>
<td class="td-top">Mobiel privé:</td>
<td><?php echo $mob; ?></td>
</tr>
<?php if( get_field('locatie') ): ?>
<tr>
<td class="td-top">Locatie:</td>
<td><?php the_field('locatie'); ?></td>
</tr>
<?php endif; ?>
</table>
</div>
</div>
</div>
</div>
<?php
}
}else{
?>
<h3>Geen collega's gevonden</h3>
<p style="font-size:1.3rem;">Probeer het opnieuw</p>
<?php } ?>
实习电话:
实习电话(额外):
电子邮件地址:
莫比尔·沃克:
Mobiel privé:
地点:
吉恩·科莱加的格沃登
Probeer-het-opnieuw
您可以分享您想要搜索的内容吗?是帖子、页面还是自定义帖子类型?很抱歉,我想使用此搜索功能在自定义帖子类型:Team
中搜索。使用wordpress搜索并稍微修改一下怎么样?或者你正在寻找答案?(旁白:如果你找到了自己问题的答案,请在下面添加答案,而不是用答案替换问题,并在所有内容中添加删除。这是一个问答网站,我们需要保留问答格式。欢迎在这里自我回答。)
<?php session_start(); ?>
<?php
/*
Template Name: Search Page
*/
?>
<?php
$value = $_POST['value'];
$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-config.php';
include_once $path . '/wp-load.php';
include_once $path . '/wp-includes/wp-db.php';
include_once $path . '/wp-includes/pluggable.php';
?>
<?php
$s=$_POST['value'];
$args = array(
'post_type' => '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);
?>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
<div style="min-height:150px;">
<div class="col-xs-12">
<h3><?php the_title(); ?></h3>
</div>
<div class="col-xs-12 col-sm-12 col-md-2 col-lg-2">
<div class="thumbnail-image">
<?php echo get_the_post_thumbnail(); ?>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-10 col-lg-10">
<div class="row">
<table class="zoek-col">
<?php if( get_field('intern_telefoonnummer') ): ?>
<tr>
<td class="td-top">Tel. Intern:</td>
<td><?php the_field('intern_telefoonnummer'); ?></td>
</tr>
<?php endif; ?>
<?php if( get_field('intern_utr') ): ?>
<tr>
<td class="td-top">Tel. Intern (extra):</td>
<td><?php the_field('intern_utr'); ?></td>
</tr>
<?php endif; ?>
<tr>
<td class="td-top">E-mail adres:</td>
<td><a href="mailto:<?php echo $mail; ?>"><?php echo $mail; ?></a></td>
</tr>
<?php if( get_field('mobiel_werk') ): ?>
<tr>
<td class="td-top">Mobiel werk:</td>
<td><?php the_field('mobiel_werk'); ?></td>
</tr>
<?php endif; ?>
<tr>
<td class="td-top">Mobiel privé:</td>
<td><?php echo $mob; ?></td>
</tr>
<?php if( get_field('locatie') ): ?>
<tr>
<td class="td-top">Locatie:</td>
<td><?php the_field('locatie'); ?></td>
</tr>
<?php endif; ?>
</table>
</div>
</div>
</div>
</div>
<?php
}
}else{
?>
<h3>Geen collega's gevonden</h3>
<p style="font-size:1.3rem;">Probeer het opnieuw</p>
<?php } ?>