Php 细枝突出显示字(木材插件)

Php 细枝突出显示字(木材插件),php,twig,timber,Php,Twig,Timber,我使用Wordpress的木材插件 我创建了一个搜索结果页面。我想突出显示用户搜索的单词 我在PHP中写道: $highlight = array(); if ($terms) { foreach($terms as $term) { array_push($highlight, '<span class="blue bold">'.$term.'</span>'); } } $highlight=array(); 如果($条款){ fo

我使用Wordpress的木材插件

我创建了一个搜索结果页面。我想突出显示用户搜索的单词

我在PHP中写道:

$highlight = array();
if ($terms) {
    foreach($terms as $term) {
        array_push($highlight, '<span class="blue bold">'.$term.'</span>');
    }
}
$highlight=array();
如果($条款){
foreach($terms作为$term){
数组推送($highlight、.$term.');
}
}
要替换PHP中搜索的单词:

<p class="date red"><?php echo str_ireplace($terms, $highlight, get_field('subtitle_post')); ?></p

您应该使用自定义细枝过滤器

来自文档:。(我试图使其适应您的示例,但您可能需要更改它)


您只需使用Twig的地图功能突出显示给定单词:

{% set sentence = 'The quick brown fox jumps over the lazy dog' %}
{% set highlight = ['the', 'quick', 'fox'] %}
{{ sentence|split(' ')|map( word => ( word|lower in highlight ? '<strong>' ~ word ~ '</strong>' : word ) )|join(' ') }}
{%set-sense='敏捷的棕色狐狸跳过了懒狗'%}
{%set highlight=['the','quick','fox']%}
{句子|拆分('')|映射(word=>(word |突出显示较低?'~word~'':word))|连接('')}
{{ yourField|highlight(words) }}
{% set sentence = 'The quick brown fox jumps over the lazy dog' %}
{% set highlight = ['the', 'quick', 'fox'] %}
{{ sentence|split(' ')|map( word => ( word|lower in highlight ? '<strong>' ~ word ~ '</strong>' : word ) )|join(' ') }}