Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter pandraka

    (@pandraka)

    Okay, I figured out how to highlight the search terms in the results.

    First I added new versions of the results templates in my child theme.

    for example posts:

    childtheme/buddypress-global-search/loop/post.php

    <?

    // first get the search term
    $key = $_GET[“s”];

    // then strip all html tags form the title and excerpt
    $title=wp_strip_all_tags(get_the_title());
    $excerpt = wp_strip_all_tags(get_the_excerpt());

    // then build the keys to highlight
    $keys = implode(‘|’, explode(‘ ‘, $key));

    //highlight both the html stripped title and excerpt fields
    $titles = preg_replace(‘/(‘ . $keys .’)/iu’, ‘<span class=”search-highlight”>\0</span>’, $title);

    $text = preg_replace(‘/(‘ . $keys .’)/iu’, ‘<span class=”search-highlight”>\0</span>’, $excerpt);

    ?>

    <!– Display the results here –>
    <h3 class=”entry-title” >
    ” title=”<?php echo esc_attr( sprintf( __( ‘Permalink to %s’, ‘buddypress-global-search’ ), the_title_attribute( ‘echo=0’ ) ) ); ?>” rel=”bookmark”><?php echo $titles; ?>
    </h3>

    <div class=”entry-content entry-summary” “>

    <?php echo $text; ?>

    </div><!– .entry-content –>

    • This reply was modified 7 years, 6 months ago by pandraka.
    Plugin Author BuddyBoss

    (@buddyboss)

    Thanks @pandraka for sharing your code snippet.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Highlight Search Terms’ is closed to new replies.