问题描述
首先,我在wordpress中使用Timber(TWIG)。
我在Ajax中创建了一个搜索查询。一切正常,但结果不适合我。有时结果显示出部分单词。
示例:如果我写“ jour ”,则会得到类似“ au jour d'hui”的结果。
我只希望整个单词都包含在内。
在这里进行研究之后。已指示使用'exact' => 1
我在查询中尝试过此方法,但现在不再有任何结果(0个结果) 每次搜索之前,我就没有好结果。
您对什么地方有任何想法吗?谢谢。
script.js
function init_datas_fetch() {
$('#search-form').on('submit',function(e) {
e.preventDefault();
var searchValue = $('#search-input').val();
if (searchValue.length > 0) {
$.ajax({
type: 'POST',url: '/wp-admin/admin-ajax.PHP',dataType: 'html',data: {
'action' : 'datas_fetch','terms' : searchValue
},success: function(data) {
$('#fetch-list').html(data);
},error: function(data) {
}
});
} else {
$('#fetch-list').html('');
}
});
}
functions.PHP
add_action( 'wp_ajax_nopriv_datas_fetch','datas_fetch' );
add_action( 'wp_ajax_datas_fetch','datas_fetch' );
function datas_fetch() {
$context['page_search'] = true;
$context['terms'] = isset($_POST['terms']) ? $_POST['terms'] : '';
$posts_types_selected = array('pages' => 'page','articles' => 'post','videos' => 'videos','podcasts' => 'podcasts');
$exclude_posts = get_field('search_exclude','option');
if ($_POST['terms']) {
$context['posts'] = Timber::get_posts(array(
'post_type' => array_values($posts_types_selected),'post_status' => 'publish','posts_per_page' => -1,'paged' => 1,'orderby' => 'date','order' => 'DESC','post__not_in' => array_values($exclude_posts),'hide_empty' => true,'has_password' => FALSE,'s' => $context['terms'],'exact' => 1
));
} else {
$context['posts'] = Timber::get_posts(array(
'post_type' => array_values($posts_types_selected),'has_password' => FALSE
));
}
Timber::render( 'bloc_fetch.twig',$context );
die();
}
tpl_search.twig
<form role="search" id="search-form">
<input type="text" id="search-input" class="search__input" placeholder="{{ __('Rechercher des articles,vidéos...','cmd') }}" value="">
<input type="submit" id="search-submit" class="search__button" value="Rechercher">
</form>
<div id="fetch-list">
{% include "bloc_fetch.twig" %}
</div>
bloc_fetch.twig
{% for card in posts %}
<div class="card">
<img src="{{ card.thumbnail.src }}" class="card__visual" alt="{{ card.thumbnail.alt }}">
<a href="{{ card.link }}" class="card__link"></a>
<div class="card__content">
{{ card.article_exceprt }}
</div>
</div>
{% endfor %}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)