![](/img/trans.png)
[英]Algolia search results page, how do dynamically select a custom content type?
[英]How do I display my Wordpress custom field attribute in the excerpt of my Algolia Search page results?
我目前在Worpdress中使用Algolia的Search插件。 我设法将一些自定义字段及其值推送到Algolia中的自定义属性。 现在,我试图在搜索结果中包含一个名为“ program_description”的自定义属性。
默认情况下,搜索仅返回“ post_title”和“ content”属性的值。 我有兴趣将自定义属性的output('program_description')替换为'content'属性。
我以为我可以通过在属性数组中添加'program_description'来简单地修改Instantsearch.php模板,如下所示:
<div class="ais-hits--content">
<h3 itemprop="name headline"><a href="{{ data.permalink }}" title="{{ data.post_title }}" itemprop="url">{{{ data._highlightResult.post_title.value }}}</a></h3>
<div class="ais-hits--tags">
<# for (var index in data.taxonomies.post_tag) { #>
<span class="ais-hits--tag">{{{ data._highlightResult.taxonomies.post_tag[index].value }}}</span>
<# } #>
</div>
<div class="excerpt">
<p>
<#
var attributes = ['program_description', '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;
}
}
relevant_content = data._snippetResult[ attributes[ 0 ] ].value;
#>
{{{ relevant_content }}}
</p>
</div>
</div>
这样做不会返回任何结果,并且会遇到以下控制台错误: Uncaught TypeError: Cannot read property 'matchedWords' of undefined
任何帮助将不胜感激。
Cannot read property 'matchedWords' of undefined
告诉我,您的attributes
自定义参数可能未在Algolia范围内定义。
我建议使用您的自定义属性(或将要使用的任何属性)初始化attributesToIndex
,这样可以确保attributesToHighlight
具有可以使用的定义。
看看这个答案 〜可能有助于阐明为什么在尝试访问_highlightResult
对象的matchedWords
成员时获得自定义属性的undefined
定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.