dqp99585 2016-05-03 11:21
浏览 27
已采纳

如何在wordpress中突出显示搜索内容中的特定单词

I want to highlight the search term in the content result in WordPress.

I tried some functions for title, excerpt and content highlight. Title and excerpt working fine but in content its not working fine. it disturb my content layout.

My actual layout is enter image description here

And after using function for highlight search term in content. It looks like this

enter image description here

The function which i use title highlight is

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;

}

And the function which i use for content is

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

}

Its working fine but break my layout.

I tried some of the jquery methods too. but no luck.

  • 写回答

1条回答 默认 最新

  • dongsisui7562 2016-05-03 13:45
    关注

    Shaban try this code your search.php and remove search_content() from functions.php

    Get the search term by this code.

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

    and then write jquery in same file with contains: function. like this

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

    That's it :)

    Hope this will help you

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作