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搜索功能结果只有一个时直接跳转到结果文章页面

Was this post helpful?

Leave a Reply