Community Forums › Forums › Archived Forums › Design Tips and Tricks › Customizing search results page for use with Relevanssi
- This topic has 24 replies, 3 voices, and was last updated 12 years ago by
Guter.
-
AuthorPosts
-
December 30, 2012 at 10:43 am #8384
lostintime
MemberTo make searching a better user experience I am using the plugin Relevanssi on my blog. Now I would like to implement Google-style “did you mean” suggestions for those situations where no results are found. The plugin author suggests adding the following line of code to the search results page in order to get that extra functionality:
<?php if (function_exists('relevanssi_didyoumean')) { relevanssi_didyoumean(get_search_query(), "Did you mean: ", "", 5); }?>
My question is: Where exactly should this piece of code go and how do I go about adding it? My theme (Streamline) does not have its own search.php file and search.php under genesis/lib/functions/ seems to deal only with the search form, not with the results page.
Any help is greatly appreciated.
December 30, 2012 at 12:12 pm #8402Bill Murray
MemberYou can add the code in the attached link to your child theme's functions.php. See the inline documentation in the code to add it just to your search page. To do that, get the page ID of your search results page (you can mouse over the URL in the WP dashboard and note the ID in the URL) and replace '1234' with your page ID.
Always be sure to have a good backup of your functions.php and the ability to put back a good copy to your server (such as with FTP) prior to making any changes, because even a small typo can break your site.
Web: https://wpperform.com or Twitter: @wpperform
We do managed WordPress hosting.
December 30, 2012 at 1:22 pm #8428lostintime
MemberThat looks great Bill. Thanks! Will try it and let you know how it goes.
Just a thought: Could I replace
if ( is_page('1234') ) {
with
if ( is_search() ) {
?
December 30, 2012 at 4:26 pm #8454Bill Murray
MemberGood thought. That change should work too. Give them both a try and post back with your results.
Web: https://wpperform.com or Twitter: @wpperform
We do managed WordPress hosting.
December 31, 2012 at 12:25 pm #8615lostintime
MemberIt works – you're brilliant Bill 🙂
Now I only have to figure out how to style the output, because the resulting string gets displayed at the top left corner of the browser window instead of *inside" the search results page, where I want it to appear.
Any suggestions on that?
Happy New Year's Eve!
December 31, 2012 at 1:36 pm #8620Bill Murray
MemberPlease up your standards for brilliance. 🙂
Have a link to your site? I can check what CSS Relevannsi is outputing with its function, which is quicker than looking in the code.
Btw, did the is_search() work?
Same to you.
Web: https://wpperform.com or Twitter: @wpperform
We do managed WordPress hosting.
December 31, 2012 at 2:20 pm #8623lostintime
MemberYes, is_search() worked fine, which makes the function a bit more generic.
Here's the link to my site: http://www.buurtaal.de/blog/
It's in German. I hope that doesn't scare you 😉 The search box is in the sidebar, there where it says "SUCHEN AUF BUURTAAL"
I used the search term "olibollen" . That should give you "Did you mean: oliebollen" in the left top corner. (The free version of Relevanssi which I am using bases the suggestions on logged queries. Since I only started usin the plugin yesterday there is currently very little data.)
Thanks again for your help, it's very much appreciated.
December 31, 2012 at 5:27 pm #8639Bill Murray
MemberI took a look and updated the code on my link. Grab an updated copy and it should fix the problem you observed.
The bad placement was because I wasn't sure where that function would do its work, so I loaded it in a less than ideal place. I've now set it to load in a more ideal place, but we'll have to see if the change suits your needs.
Post back with the results.
Web: https://wpperform.com or Twitter: @wpperform
We do managed WordPress hosting.
January 1, 2013 at 4:20 am #8698lostintime
MemberYou're my coding hero Bill 😉
I ended up changing the hook to genesis_after_content because using genesis_before_content displayed the result in the sidebar.
Thank you so much for pointing me in the right direction.
January 1, 2013 at 8:37 am #8722Bill Murray
MemberHmmm...after thinking about this more, the genesis_after_post_content hook might be even better. genesis_after_content isn't quite right, which is why the text is so far to the left.
Web: https://wpperform.com or Twitter: @wpperform
We do managed WordPress hosting.
January 1, 2013 at 9:26 am #8725lostintime
MemberI tried that, but unfortunately genesis_after_post_content made the string disappear .... Could that be because the search results are not displayed in a post?
Using genesis_after_content seems to bring the best result – even though it isn't pretty because of the missing left margin.
Would it be possible to style the output using css? Or would you consider that a hack?
January 1, 2013 at 10:06 am #8734Bill Murray
MemberBefore we go the CSS route, let's try this:
add_action('genesis_after_post_content', 'wpp_relevanssi_did_you_mean', 5);
Web: https://wpperform.com or Twitter: @wpperform
We do managed WordPress hosting.
January 1, 2013 at 10:17 am #8736lostintime
MemberBummer, no joy unfortunately. The string doesn't display anywhere on the page.
January 1, 2013 at 10:34 am #8737Bill Murray
MemberLet's not give up yet...
Does Relevanssi create a page for search results? Can you try the code in my last example using the is_page() with the page ID? I'm wondering if the is_search() thing is breaking it.
The addition of the priority (the 5 parameter) should have fixed the "not displaying" issue.
Web: https://wpperform.com or Twitter: @wpperform
We do managed WordPress hosting.
January 1, 2013 at 12:40 pm #8764lostintime
MemberThe thing is that I there doesn't seem to be a separate page for displaying the search results. At least there's nothing in the Streamline backend. That's what's preventing me from using is_page() with the page ID.
January 1, 2013 at 1:24 pm #8787Bill Murray
MemberI updated the code on the link. Let's see if this gets closer. I added a priority because right now this is getting added after the sidebar. Then, I gave the strings some CSS based on Relevanssi's documentation.
I gave it an ID of didyoumean, so you'll need to add something like this to your child theme's style sheet:
/* Relevanssi search result when nothing found */ #didyoumean { margin: 0 0 10px; padding: 0 30px; }
That ought to make it line up with the other content. Adjust to suit your taste. Keep your fingers crossed 🙂
Web: https://wpperform.com or Twitter: @wpperform
We do managed WordPress hosting.
January 1, 2013 at 3:14 pm #8816lostintime
MemberWow, this is interesting. The updated code made the result appear at the top of the sidebar again. When I changed genesis_after_content back to genesis_before_content it went back into the post content area. With the css in place it now looks perfect.
A big thanks for all your time and advice!
January 1, 2013 at 5:58 pm #8853Bill Murray
MemberGlad it works. It looks good.
I think the priority caused the genesis_after_content not to work. The default priority is 10, and lower numbers mean higher execution priority, so maybe 5 was too low. You can delete the priority altogether, since it's optional. I don't understand the on-screen text, but if the order isn't to your liking, you can try going back to the after content hook and upping the priority. That might put this text after the content, which I suspect says that the string wasn't found. If you use use a tool like Firebug or right click on an element in Chrome and inspect it, you can see where it puts that ID, and the goal is to get it after the content but before the sidebar. The CSS won't change.
Web: https://wpperform.com or Twitter: @wpperform
We do managed WordPress hosting.
January 2, 2013 at 7:07 am #8927lostintime
MemberThanks for your thoughts! Will give it a try.
January 2, 2013 at 7:21 am #8930lostintime
MemberYep, I upped to priority to 10, changed the hook back to genesis_after_content and now everything is the way I want it to look.
I can't thank you enough Bill for all your hand holding through this process.
-
AuthorPosts
- The topic ‘Customizing search results page for use with Relevanssi’ is closed to new replies.