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

Suchbegriff und Ergebnisanzahl in WordPress anzeigen

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.

In diesem Tutorial zeigen wir Ihnen, wie Sie eine einfache Funktion auf Ihrer Suchseite hinzufügen können, die den Suchbegriff und die Anzahl der Ergebnisse anzeigt. Diese Funktion war ein spezieller Wunsch eines unserer Benutzer per E-Mail. Wenn Sie möchten, dass wir ein Thema behandeln, können Sie uns gerne einen Vorschlag machen.

Displaying search term and result count in WordPress search

Öffnen Sie die Datei search.php in Ihrem Thema und fügen Sie den folgenden Code ein:

<h2 class="pagetitle">Search Result for <?php /* Search Count */ $allsearch = new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e(''); _e('<span class="search-terms">'); echo $key; _e('</span>'); _e(' &mdash; '); echo $count . ' '; _e('articles'); wp_reset_query(); ?></h2>

Der obige Code wird in etwa so angezeigt:

Suchergebnis für twitter – 15 Artikel

Sie können den Suchbegriff auch hervorheben, indem Sie die CSS-Klasse .search-terms in das Stylesheet Ihres Themes einfügen. Hier ist ein einfaches CSS, um Ihnen den Einstieg zu erleichtern:

.search-terms {
background-color:yellow;
color:blue;
}

Dies ist nur eine der vielen Möglichkeiten, die Sie für Ihre Suchseite nutzen können, wenn Sie sie anpassen. Sie können auch Suchbegriffe in den Ergebnissen hervorheben und sogar eine Suche nach Kategorien zu Ihrer WordPress-Suche hinzufügen.

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

16 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. Aniruddh says

    Can I use this code in the sidebar.php file? As I need to show the search term and result count in the sidebar. Please help.

  3. Marco says

    thanks a lot for this snippet! :)

    according to the wp codex the function wp_specialchars is deprecated. it says that you should use esc_html instead.

    regards,
    marco

  4. Henry says

    echo $wp_query->found_posts;
     
    Adding this to the top of your search page will display the number of posts found for a particular search.
     
    echo get_search_query();
     
    This will display the search term (entered in to the search box by your site visitor).
     
    timer_stop(1);
     
    This will display the time it took to run the search.
     
    All together you could output something like:
     
    Your search for „coffee beans“ returned „16“ posts in „0.87“ seconds.

  5. Chris Murphy says

    Wow. It’s a lot simpler than that to count WordPress‘ search results, here:

        global $wp_query;
        $count = sizeof( $wp_query->posts );
        echo"<pre>";
        //var_dump($wp_query);// The WP Query Object
        var_dump($wp_query->posts); // The WP Query Object's 'posts' property (note the plural)
        echo"</pre>";
    

    If you drop that snippet in your search template, it will output the properties of the ‚$wp_query‘ variable, specifically the ‚posts‘ property of the object. This is an array that stores a collection of the returned posts from the search query, which you can do a number of things with once you access it, including (*drumroll*), *count* the number of posts in that array using either PHP’s ‚count()‘ or ’sizeof()‘ functio

    • Ilya says

      Chris,

      This won’t work. The point is to find out the total number of results matching the query — not the number currently fetched. If there are more results than „fits“ within a page, your code will only get number of elements on a page.

  6. Konstantin says

    Good idea. Bad execution.
    Why in the world would you want to translate a span tag???
    And my favorite: _e(''); Huh??
    Why would you want to query the same search twice?
    Doesn’t make sense at all.

    But I don’t want to hate, this is how it should be done (in my humble opinion):


    post_count); ?>

  7. Tom says

    I really don’t give my on page search enough thought. Something like this is great because it makes the page more personalised.

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.