【问题标题】:How to highlight a specific word in a searched content in wordpress如何在wordpress中突出显示搜索内容中的特定单词
【发布时间】:2016-05-03 11:21:30
【问题描述】:

我想在 WordPress 的内容结果中突出显示搜索词。

我尝试了一些标题、摘录和内容突出显示的功能。标题和摘录工作正常,但内容不正常。它扰乱了我的内容布局。

我的实际布局是

在使用功能突出显示内容中的搜索词之后。看起来是这样的

我使用标题高亮的功能是

function search_title() {
$title = get_the_title();
$keys = implode('|', explode(' ', get_search_query()));
$content = strip_tags($content);
$title = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $title);
echo $title;

}

我用于内容的功能是

function search_content() {
    $content = get_the_content();
    $keys = implode('|', explode(' ', get_search_query()));
    // $content = strip_tags($content);
    $content = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $content);
    $content = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', '', $content);
    echo '<p>' . $content . '</p>';

}

它工作正常,但破坏了我的布局。

我也尝试了一些 jquery 方法。但没有运气。

【问题讨论】:

  • 有关 jQuery 解决方案,请查看 mark.js!

标签: php jquery regex wordpress


【解决方案1】:

Shaban 试试这个代码你的 search.php 并从 functions.php 中删除 search_content()

通过此代码获取搜索词。

<?php echo $str = esc_html( get_search_query( false ) ); ?>

然后用 contains: 函数在同一个文件中写入 jquery。像这样

$( ".content-column:contains('<?php echo $str ?>')" ).css( "background", "Yellow" );

就是这样:)

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 2012-05-21
    • 2020-10-11
    相关资源
    最近更新 更多