Wordpress directly jumps to this article page when there is only one article in the category list or search result

Source: Internet
Author: User
Tags php file

When the category list contains only one article, the article page is displayed.


When there is only one article under the category, you cannot directly jump to the article page. The answer is yes, and the method search result is a bit, the difference is that you can change the current page to the search result page to the category list page. The modified code is as follows.

Add the following code to the topic's functions. Php file:


Function category_jump_page () {// define a function
If (is_category () {// determines if it is a category list page
Global $ wp_query; // defines global variables
If ($ wp_query-> post_count = 1) {// determines whether the number of articles is 1
Wp_redirect (get_permalink ($ wp_query-> posts ['0']-> ID); // if it is 1, use the wordpress built-in function wp_redirect to redirect
  }
 }
}
Add_action ('Template _ redirect ', 'Category _ jump_page'); // use the defined function

If you want to apply the code to the search result page at the same time, you can modify the code as follows:


Function category_jump_page () {// define a function
If (is_category () | is_search () {// determines whether it is a category list page or a search result page
Global $ wp_query; // defines global variables
If ($ wp_query-> post_count = 1) {// determines whether the number of articles is 1
Wp_redirect (get_permalink ($ wp_query-> posts ['0']-> ID); // if it is 1, use the wordpress built-in function wp_redirect to redirect
  }
 }
}
Add_action ('Template _ redirect ', 'Category _ jump_page'); // use the defined function


If there is only one search result, you can directly jump to the results article page.

When you use the wordpress built-in search box to search, when there is only one search result, you can directly jump to the result page without displaying the search result page. The advantage of this effect is that the search results are needed by the user and can improve the user experience. The disadvantage is that the search results are not needed by the user and are not conducive to the user experience.

Implementation code:

Function search_jump_page () {// define a function
If (is_search () {// determines if it is a search result page
Global $ wp_query; // defines global variables
If ($ wp_query-> post_count = 1) {// determines whether the number of search results is 1
Wp_redirect (get_permalink ($ wp_query-> posts ['0']-> ID); // if it is 1, use the wordpress built-in function wp_redirect to redirect
  }
 }
}
Add_action ('Template _ redirect ', 'search _ jump_page'); // use the defined function
Add the above code to the topic's functions. Php file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.