【问题标题】:Wordpress search issueWordPress 搜索问题
【发布时间】:2021-09-13 21:14:30
【问题描述】:

嗨,我有一个网站,当我在其上搜索内容时,我在主页上有一个搜索栏,它会显示与该关键字相关的数字结果章节/帖子,但是当我打开任何章节/帖子时,有一些章节页面中的“下拉菜单”,我看不到我搜索过的突出显示的单词。任何人都可以帮助突出显示文本或其背景

例如,我在此页面 https://policies.americanprep.org/ 上搜索词 “Morning” 我会在此页面上制作背景或文字颜色 “红色” https://policies.americanprep.org/chapter-f-classroom-organization/?highlight=Morning?s=Morning

【问题讨论】:

  • 最好贴一些代码

标签: javascript php wordpress wordpress-gutenberg wordpress-shortcode


【解决方案1】:

您需要在结果页面中更改默认的 wp_content/wp_excerpt,如下所示:

if (isset($_GET['highlight'])){
    $content = get_the_content();
    $keys = implode('|', explode(' ', $_GET['highlight']));
    $content = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $content);

    echo '<p>' . $content . '</p>';
} else {
    echo '<p>' . get_the_content() . '</p>';
}

还需要为span.search-highlight添加css代码

$_GET 参数's' 最好从url中删除。

【讨论】:

    猜你喜欢
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多