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

Hinweis der Redaktion: Wir erhalten eine Provision für Partnerlinks auf WPBeginner. Die Provisionen haben keinen Einfluss auf die Meinung oder Bewertung unserer Redakteure. Erfahre mehr über Redaktioneller Prozess.

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

Offenlegung: Unsere Inhalte werden von unseren Lesern unterstützt. Das bedeutet, dass wir möglicherweise eine Provision verdienen, wenn Sie auf einige unserer Links klicken. Mehr dazu erfahren Sie unter Wie WPBeginner finanziert wird , warum das wichtig ist und wie Sie uns unterstützen können. Hier finden Sie unseren redaktionellen Prozess .

Das ultimative WordPress Toolkit

Erhalte KOSTENLOSEN Zugang zu unserem Toolkit - eine Sammlung von WordPress-bezogenen Produkten und Ressourcen, die jeder Profi haben sollte!

Reader Interactions

17 KommentareEine Antwort hinterlassen

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Imme says

    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 says

    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 says

      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 says

        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. Steve says

    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.

  5. DauAnunturi says

    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.

Eine Antwort hinterlassen

Danke, dass du einen Kommentar hinterlassen möchtest. Bitte beachte, dass alle Kommentare nach unseren kommentarpolitik moderiert werden und deine E-Mail-Adresse NICHT veröffentlicht wird. Bitte verwende KEINE Schlüsselwörter im Namensfeld. Lass uns ein persönliches und sinnvolles Gespräch führen.