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.