WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Highlight Search Results (5 posts)

  1. hunnsdon
    Member
    Posted 11 months ago #

    I found this function on the net. I have placed this in my custom functions file separate from functions.php.

    function highlight_search_term($text){
        if(is_search()){
    		$keys = implode('|', explode(' ', get_search_query()));
    		$text = preg_replace('/(' . $keys .')/iu', '<span class="search-term">\1</span>', $text);
    	}
        return $text;
    }
    add_filter('the_excerpt', 'highlight_search_term');
    add_filter('the_title', 'highlight_search_term');

    It works fine except it alters the read more link. Hovering over this link shows:

    http://www.mysite.com/%3span%20classs=

    When it is removed the link is unaffected. What is wrong with it? I have used the following CSS with it:

    .search-excerpt {
    	background: yellow;
    }
  2. Tara
    Member
    Posted 11 months ago #

    first off, your code indicates class name "search-term",but your css indicates class name to be "search-excerpt"

  3. hunnsdon
    Member
    Posted 11 months ago #

    Hi Tara

    Thanks for that.

    I had two entries in the CSS doing the same job. Removed the wrong entry but still the problem remains.

    Are you a coder, can you help further?

    Ray

  4. Tara
    Member
    Posted 11 months ago #

    I am not a coder. You may want toask here: http://wordpress.stackexchange.com/

    If you get the answer, please report back so that someone with similar issue can benefit from it.

  5. hunnsdon
    Member
    Posted 9 months ago #

    Yes, I tried and got an answer from stack exchange, but still had problems.

    Decided to use a plugin: 'Highlight Search Terms'

Reply

You must log in to post.

About this Topic