【发布时间】:2018-04-28 21:57:32
【问题描述】:
有没有办法将搜索限制为仅在 Wordpress 上使用 Algolia 发布标题?
我需要这个来实现自动完成功能。
【问题讨论】:
有没有办法将搜索限制为仅在 Wordpress 上使用 Algolia 发布标题?
我需要这个来实现自动完成功能。
【问题讨论】:
您绝对可以自定义搜索所基于的字段。
首先,您必须通过更改 attributesToIndex 设置让 Algolia 知道您只想在帖子标题字段上进行搜索:
/**
* @param array $settings
*
* @return array
*/
function custom_posts_index_settings( array $settings ) {
$settings['attributesToIndex'] = array( 'unordered(post_title)' );
return $settings;
}
add_filter('algolia_posts_index_settings', 'custom_posts_index_settings');
add_filter('algolia_searchable_posts_index_settings', 'custom_posts_index_settings');
然后您需要自定义自动完成的建议模板,首先将 autocomplete.php 文件移动到您的主题文件夹中,如本指南中所述:https://community.algolia.com/wordpress/customize-autocomplete.html
基本上你会删除看起来像这样的部分:
<#
var attributes = ['content', 'title6', 'title5', 'title4', 'title3', 'title2', 'title1'];
var attribute_name;
var relevant_content = '';
for ( var index in attributes ) {
attribute_name = attributes[ index ];
if ( data._highlightResult[ attribute_name ].matchedWords.length > 0 ) {
relevant_content = data._snippetResult[ attribute_name ].value;
break;
} else if( data._snippetResult[ attribute_name ].value !== '' ) {
relevant_content = data._snippetResult[ attribute_name ].value;
}
}
#>
【讨论】: