duanci1858 2016-10-19 15:22
浏览 80

如何在Algolia Search页面结果的摘录中显示我的Wordpress自定义字段属性?

I'm currently using Algolia's Search plugin within Worpdress. I've managed to push some custom fields and their values to custom attributes within Algolia. Now, I'm trying to include a custom attribute named 'program_description' in my search results.

By default, the search is only returning the value of the 'post_title' and 'content' attributes. I'm interested in replacing the 'content' attribute output with with my custom attribute's output('program_description').

I thought I would simply modify the instantsearch.php template by adding 'program_description' to the attributes array like so:

<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>

In doing so, none of my results are returned and I'm met with the following console error: Uncaught TypeError: Cannot read property 'matchedWords' of undefined

Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dougong8012 2016-10-19 15:44
    关注

    Cannot read property 'matchedWords' of undefined tells me that your custom parameter for attributes may not be defined in the scope of Algolia.

    I would suggest initializing attributesToIndex with your custom attributes (or any attributes you'll be using)- that way you can ensure that attributesToHighlight has definitions to work with.

    Check out this answer~ it might help shed light on why you're getting an undefined definition for your custom attribute while trying to access the matchedWords member of your _highlightResult object.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable