New Landing How can we help? General Forum Help Underline search results

Viewing 6 posts - 1 through 6 (of 6 total)
  • #167216
    Antonello Torzillo
    Member
    Post count: 3

    Hi,
    I have a question about the possibility to underline ( or give it some styles ) the words that result by search. I tried all methods seen online ( I wouldn’t like using plugins if it is possible! ) but works only on title ( the_title ). on excerpt ( the_excerpt ) don’t work. Have you any ideas to do this?

    Thanks in advance,
    Antonello

    I put this code in function.php:

    function highlight_results($text){

    if(is_search()){
    $sr = get_query_var(‘s’);
    $keys = explode(” “,$sr);
    $text = preg_replace(‘/(‘.implode(‘|’, $keys) .’)/iu’, ‘<strong class=”search-term”>’.$sr.’‘, $text);
    }

    return $text;
    }
    add_action(‘the_excerpt’, ‘highlight_results’);
    add_action(‘the_title’, ‘highlight_results’);

    #167222
    Rui Guerreiro – SUPPORT
    Keymaster
    Post count: 25779

    Hi,

    Not understanding exactly what you are trying to achieve.
    Do you have any example?

    -Rui

    #167296
    Antonello Torzillo
    Member
    Post count: 3

    Hi Rui,
    Thanks for the fast answer.
    Yes, I can upload an image. As you can see, the search results are underlined only on titles. I would like the same result for the excerpt. I read online that the code works but not in my case, and i don’t understand why. If you can tell me something about that I’m grateful for help.

    Attachments:
    You must be logged in to view attached files.
    #167306
    Kyle – SUPPORT
    Moderator
    Post count: 35880

    So you want to underline all instances of ‘paypal’?

    – Kyle

    #167311
    Antonello Torzillo
    Member
    Post count: 3

    Hi,
    yes, exactly.

    Antonello

    #167321
    Kyle – SUPPORT
    Moderator
    Post count: 35880

    That wouldn’t be possible, to apply css to elements they have to have an individual class, search results do not get assigned an individual class for the words you search, so it’s not possible to apply css to them

    – Kyle

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in and have valid license to reply to this topic.

License required for one of the following items
Login and Registration Log in · Register