When there is only one article in the WordPress site search result, it will automatically jump to that article.

888u

Last update at :2024-05-10,Edit by888u

In-site search is a necessary function of the website, and the WordPress system does a pretty good job in the search function. Sometimes when the number of articles is small or the search keywords are complex, the matching search results may only be one article. If the sidebar is set to be longer, it will appear a little "left-right imbalance" and not beautiful. Considering the user experience, let’s make a small optimization for WordPress search: when the WordPress search results only have one article, we let the page automatically adjust to this article.

In this way, it avoids the situation of a single article in the search list and a long sidebar; it also saves the user a second click, which can improve the user experience to a certain extent.

And we only need to add a piece of code to the theme function template functions.php.

//Jump to the article when there is only one article in the search results
add_action('template_redirect', 'redirect_single_post');
function redirect_single_post() {
if (is_search()) {
global $wp_query;
if ($wp_query->post_count == 1 && $wp_query->max_num_pages == 1) {
wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
exit;
}
}
}

Recommended site searches: domain name registration information query, website filing time, com domain name registration 1 yuan, domain name registration query US server website, free mysql space, domain name reservation, Chinese domain name fee, Qingdao ip agency, telecom hosting,

p>

When there is only one article in the WordPress site search result, it will automatically jump to that article.

All copyrights belong to 888u unless special state
取消
微信二维码
微信二维码
支付宝二维码