當你的讀者在你的WordPress博客的內置的搜索引擎搜索時候,返回的結果是按照時間先后的陳列格式。如果搜索結果只有一篇文章的時候,為提高用戶體驗,我們可以讓著唯一的一個搜索結果自動跳轉到該文章。下面就是所需要的代碼。
將下面的代碼添加到主題的funtions.php 文件的最后一個 ?> 前:
- add_action('template_redirect',?'redirect_single_post');
- function?redirect_single_post()?{
- ????if?(is_search())?{
- ????????global?$wp_query;
- ????????if?($wp_query->post_count?==?1)?{
- ????????????wp_redirect(?get_permalink(?$wp_query->posts['0']->ID?)?);
- ????????}
- ????}
- }
聲明:
教程轉自:DeveWork (轉載此文章僅為學習交流只用,如有異議請聯系本站刪除,謝謝!)