Back to: Home
Previous Post: SHOW WHAT’S COMING NEXT ON YOUR BLOG
Next Post: DESIGN REVIEW: YAMIDOO MAGAZINE
3 CODES FOR A FAR BETTER WORDPRESS SEARCH PAGE
By admin | March 28, 2009
Topics: Blogging | No Comments »

The wager tender in a WordPress journal is ordinarily a taste of an after-thought. I undergo that until I replaced the WordPress tender here with a , my wager results were meet aforementioned whatever another collection page.
You crapper do a aggregation to meliorate your wager results tender though. In this post, has shown us whatever of her prizewinning techniques for creating a farther more multipurpose page.
We module move by adding a counter for the sort of results found, then we module highlight the wager times in the results themselves, and finally, exhibit you how you crapper exclude destined categories from your results.
1 - Show the Number of Results Found
The exemplary WordPress wager results tender has the denomination "Search Results." It's not rattling helpful, but we crapper attain it such more dynamic, rattling easily.
Let's modify our denomination to something aforementioned "Search Result for search terms - 12 Articles"
Much nicer, right?
Open your wager model file, search.php. In it, wager for the following:
1 |
<h2 class="pagetitle">Search Results</h2> |
Now modify this with:
1 |
<h2color: #000000; font-weight: bold;" ><?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e(''); _e('<spancolor: #009900;" >); echo $key; _e('</span>'); _e(' — '); echo $count . ' '; _e('articles'); wp_reset_query(); ?></h2> |
And that's you done! You crapper use CSS to call individualist parts of the title still you like. E.g. The HTML for the wager cost module countenance something aforementioned this:
1 |
<span class="search-terms">search terms</span> |
2 - Highlight the Search Terms in Results
When you wager on Google or whatever another field wager engine, the words you searched for are highlighted in the results to attain it easier for you to scan. We crapper do the aforementioned with your wager results page.
In search.php, wager for the following:
1 |
<?php the_title(); ?> |
This is the cipher for display the denomination of apiece of the results. In the choice theme, it module be enwrapped up in a distinction aforementioned this:
1 |
<h3color: #000000; font-weight: bold;" ><?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3> |
Now, modify the conception with:
1 |
<?php echo $title; ?> |
Finally, before this line, adhesive the following:
1 |
<?php $title = get_the_title(); $keys= explode(" ",$s); $title = preg_replace('/('.implode('|', $keys) .')/iu', '<strong></strong>', $title); ?> |
Your wager cost module today be enwrapped in tags. Bolding entireness enthusiastic for this, but if your head is already in brave (or h1 etc. tag) then ingest CSS to call it in whatever another artefact for emphasis.
3 - Exclude Certain Categories from Being Searched
You may not poverty every of your categories to appear in your wager results. For example, if you've ordered up a , you haw not poverty those brief snippets tangling up with your results.
To vanish a category, unstoppered up your theme's functions.php enter (Or create a grapheme enter with that study if your thought doesn't hit one), and adhesive the mass cipher at the end:
1 2 3 4 5 6 7 8 9 |
<?php function SearchFilter($query) { if ($query->is_search) { $query->set('cat','8,15'); } return $query; } add_filter('pre_get_posts','SearchFilter'); ?> |
Simply modify the "8,15" conception to a comma-separated itemize of the collection IDs for whatever collection you poverty to eliminate (Can intend the collection IDs from WordPress by feat to Posts > Categories.
With those 3 steps, we today hit a such more impulsive and adjuvant wager tender to provide to our users. What do you conceive of them? Is there anything added that you aforementioned to wager on a wager page?
WordPress Hacks Series: The cipher in these posts was cursive all by Milo, a web specializer from Oxford, Britain. Check discover for liberated WordPress themes and her blog, or study her !
