wp-content » themes » functions.php » If there is only one search result, skip the results page and jump directly to the content 🔧

If there is only one search result, skip the results page and jump directly to the content 🔧

Add the following to your theme functions.php file:


function search_jump_page() {
 if(is_search()) {
  global $wp_query;
  if ($wp_query->post_count == 1) {
   wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
  }
 }
}

add_action('template_redirect', 'search_jump_page');

Source: WordPress搜索功能结果只有一个时直接跳转到结果文章页面

See also  Change Text Highlight Color 🎨 in WordPress without any plugin

Was this post helpful?

Leave a Comment

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.

Recommended