New great code to improve site search
As is known “Than the user takes fewer steps towards the goal, the more likely, that he will reach her (with)“. The goal can be the design of goods in an online store or, as in our case, this is a search on the site.
Ask yourself an interesting question. If a user searches for a query on your blog and only finds one result, should he be forced to go through this paragraph? In my opinion, it's better to do that, so that the program simply redirects the user to the article he needs.
Sample code to improve this type of search:
1 2 3 4 5 6 7 8 9 |
add_action('template_redirect', 'single_result'); function single_result() { if (is_search()) { global $wp_query; if ($wp_query->post_count == 1) { wp_redirect( get_permalink( $wp_query->posts['0']->ID ) ); } } } |
Checked everything, code works fine. Good luck with development.
/*
- Basic web design course;
- Site layout;
- General course on CMS WordPress and continuation of the course on template development;
- Website development in PHP.