如何在我的Algolia搜索页面结果的摘录中显示我的Wordpress自定义字段属性
How do I display my Wordpress custom field attribute in the excerpt of my Algolia Search page results?
我目前在Worpdress内使用Algolia的搜索插件。我已经成功地将一些自定义字段及其值推送到Algolia中的自定义属性。现在,我试图在我的搜索结果中包含一个名为"program_description"的自定义属性。
默认情况下,搜索只返回'post_title'和'content'属性的值。我有兴趣用我的自定义属性的输出('program_description')替换'content'属性输出。
我想我会简单地修改instantsearch.php模板,将'program_description'添加到属性数组中,如下所示:
<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
定义
相关文章:
- 角度图表;t显示在我的页面中
- 将日期时间作为 JSON 发送将无法在我的视图中正确显示
- 我希望只有在我滚动页面后才能显示我的返回页首图标
- 在我的网站上显示最近提交的文章/图片
- 在主页上时,我的整个导航都会突出显示
- 使用API在我的网站Google-Map上显示搜索地址的纬度和经度
- 当我的条件为true时,显示一条弹出消息
- 为我的rails应用程序显示javascript弹出消息
- 为什么我的视图没有显示在角度ui视图中
- 我的时间倒计时脚本在谷歌浏览器中工作正常,但在其他浏览器中打开时它显示为南楠
- 为什么我的脚本正在运行动画,但没有显示它
- 我如何才能在点击按钮时将我的数字和财富显示在这个脚本上
- 列表没有显示在我的本地主机上,但显示在jsfiddle上
- 为什么我的Alexa技能测试显示正确的lambda输出,但在开发人员控制台中测试时却给出错误消息
- 我的localStorage结果显示了[null,null,“something”]],而不是“”;什么”;
- 希望在单击href链接时显示我的广告
- 我的复选框没有't使用的循环来显示数据
- 我的显示/隐藏功能代码出了什么问题
- 为什么我的显示/隐藏脚本不工作
- 我可以连接到外部服务器和改变我的显示页面