W tym poradniku pokażemy, jak dodać prostą funkcję na twojej stronie wyszukiwania, która wyświetli wyszukiwany termin i liczbę wyników. Ta funkcja była specjalną prośbą jednego z naszych użytkowników przesłaną e-mailem. Jeśli chcesz, abyśmy omówili jakiś temat, nie krępuj się zasugerować.
Otwórz plik search.php w twoim motywie i dodaj następujący kod:
<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(' — '); echo $count . ' '; _e('articles'); wp_reset_query(); ?></h2>
.
Powyższy kod wyświetli coś takiego:
Wynik wyszukiwania dla twitter – 15 artykułów
Możesz również wyróżnić wyszukiwany termin, dodając klasę CSS .search-terms do arkusza stylów twojego motywu. Oto prosty kod CSS na początek:
.search-terms { background-color:yellow; color:blue; }
.
To tylko jedna z fajnych rzeczy, które możesz zrobić dla swojej strony wyszukiwania, dostosowując ją do własnych potrzeb. Możesz również wyróżnić wyszukiwane terminy w wynikach, a nawet dodać funkcję wyszukiwania według kategorii do twojego wyszukiwania WordPress.
Źródło: Michael Martin
Syed Balkhi
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!
Aniruddh
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.
Marco
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
Marco
Next time I take a close look to the comments. CHADHAYTON already posted a link how you can handle it now.
Henry
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.
jameslhf
Thank you Henry, that helped me!
Prasanna
Thanks Henry. This helped me a lot.
chadhayton
It is hard to tell how old this post is, but WordPress 3.1 includes a way to get the total search results that doesn’t require a separate query:
http://codex.wordpress.org/Creating_a_Search_Page#Display_Total_Results
Chris Murphy
Wow. It’s a lot simpler than that to count WordPress’ search results, here:
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
Editorial Staff
Your code is cut off a little. Is there a way that you can email us the snippet and we can try it and write about it. Obviously full credit will be given to you.
Administrator
Ilya
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.
pete
Search results are a personal thing from a user so it’s good if they are given lots of info for them.
basilakis
So the code is right or not?
I see Konstantin has some point about the query, but the comments „ate” the results!
Editorial Staff
This code is correct. It works, the only issue he is pointing out is that it can be done differently as well. We have used this code on a client’s website and it works.
Administrator
Konstantin
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); ?>
Tom
I really don’t give my on page search enough thought. Something like this is great because it makes the page more personalised.