Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

Wie man die Suchbegriffe in den Ergebnissen in WordPress hervorhebt

Um Ihre WordPress-Suche noch benutzerfreundlicher zu gestalten, können Sie die Suchbegriffe in den Ergebnissen hervorheben. Wir haben dies für einen unserer Kunden getan und dachten, dass es auch für andere Benutzer nützlich sein könnte. In diesem Artikel zeigen wir Ihnen, wie Sie Suchbegriffe in den Ergebnissen in WordPress hervorheben können.

Highlighting search terms in WordPress search results

Öffnen Sie zunächst Ihre search.php und suchen Sie den folgenden Code:

<?php the_title(); ?>

Ersetzen Sie den obigen Code durch:

<?php echo $title; ?>

Stellen Sie sicher, dass Sie diese Zeile über dem Titelcode einfügen:

<?php $title = get_the_title(); $keys= explode(" ",$s); $title = preg_replace('/('.implode('|', $keys) .')/iu', '<strong class="search-excerpt">\0</strong>', $title); ?>

Öffnen Sie nun Ihre CSS-Datei und fügen Sie das Styling für die Klasse search-excerpt hinzu, damit der Begriff hervorgehoben wird. Im Moment macht der Code die Suchbegriffe fett. Sie können dieses einfache CSS im Stylesheet Ihres Themes ausprobieren.

strong.search-excerpt { 
background-color:yellow;
color:blue;
}

Quelle: Michael Martin

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

16 KommentareLeave a Reply

  1. Rachelle

    This works great but how do you highlight the search term in the excerpt and not just the title?

  2. Imme

    Hej, thank you very much for this very helpful piece of code. Is there a way to exclude links? The code as is breaks many of the “more”-links on my site.

  3. Vernon Fowler

    Any chance you can update this neat tutorial for modern themes such as Twenty Seventeen where instead of title() in the loop, the loop goes through:

     get_template_part( 'template-parts/post/content', 'excerpt' );

    I’m comfortable with replacing

    <strong class="search-excerpt">\0</strong>

    with HTML5

    <mark>\0</mark>

    and the relevant CSS.

    Or will we need a different approach in themes using get_template_part ?

    • WPBeginner Support

      Hey Vernon,

      Thanks for the suggestion. We will try to update the article with more detailed instructions.

      Meanwhile, you will need to edit the /template-parts/content-search.php template. If your theme does not have it, then you can create it and then reference it in your search.php template.

      Admin

      • Vernon Fowler

        That’s working. Thank you.

        Can we do the same for the_content as for the the_title or do we need a different approach?

  4. Wayan Cenik

    Thanks a lot for the code, is work perfectly
    I just the code at function, and done, is work

  5. Marlene

    Hi

    I can´t find on my site. I have this:

    How can I change someting in that?

    Thanks
    Marlene

  6. Steph

    There is no “” in my search.php file..
    —————————–
    Mine looks like this:

    “”

    • WPBeginner Support

      Seems like you wanted to paste code and it stripped out. Please wrap your code around [php] [/php] tags

      Admin

  7. Steve

    This doesn’t even come close to working. All it does is display the title of the page the search term is found on. The terms themselves aren’t wrapped in any tags whatsoever.

    • nate

      Actually, it works perfectly. I don’t think you know what you are doing.

  8. Jason

    Thanks! Works like a charm!

  9. Chris

    You should add a little example image on every tutorial, that would be more understandable (:

  10. DauAnunturi

    Nice tutorial. And for those wo want to make some highlight with colors they must define their css class as div.highlight
    and replace the code with
    And that`s alll. Thanks and have fun.

  11. Nina

    Very helpful tutorial, will use it later on :)
    Thanks for sharing!

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.