trying to implement 'i'm feeling lucky' wordpress search
-
Hi All,
This is a bit of a technical question really, for any php developers out there.I’m currently trying to implement a change to the wordpress search results page whereby if there is only one result, the user is automatically redirected to that result page.
The way i’m trying to do this is by adding code like this to the top of my search.php page, basically i’m working out whether there is only one result, if there is only one i get the link and then use the header redirect to change the page:
<?php if (count($posts)==1){ while (have_posts()){ the_post(); $linky = the_permalink(); } header("Location:$linky"); } ?>It’s throwing an error about an unmatched endwhile on line 12 which doesn’t make any sense to me. Is this a good way to proceed, or does anyone know a better way of getting the same type of result?
I think that maybe it’s too late to redirect if i put this in the search.php file because it’s already started rendering the result. Could anyone confirm that?
I’ve only just started writing php but i come from a background in Java so i understand the overall concepts but just need some help with syntax and symantics.
any help much appreciated.
Mark
-
You did not specify a blog address or reason for posting when you created this topic.
This support forum is for blogs hosted at WordPress.com. If your question is about a self-hosted WordPress blog then you’ll find help at the WordPress.org forums.
If you don’t understand the difference between WordPress.com and WordPress.org, you may find this information helpful.
If you forgot to include a link to your blog, you can reply and include it below. It’ll help people to answer your question.
This is an automated message.
- The topic ‘trying to implement 'i'm feeling lucky' wordpress search’ is closed to new replies.