任何问题请联系WX:uu16853

买模板送网站采集器 让你轻松放开双手运营网站!

购买更多有优惠!

WordPress搜索结果只有一篇文章时自动跳转到文章

当读者通过WordPress自身的搜索功能来搜索文章时,如果返回的结果只有一篇文章,我们可以直接让它跳转到这篇文章,提高用户体验。 实现的方法很简单,只需要在你主题的 functions.php 文件中添加下面的代码: 1 2 3 4 5 6 7 8 9 10 add_action(’template_redirect’, ‘redirect_single_post’); function redirect_single_post() { if (is_search()) { global $wp_query; if ($wp_query->post_count == 1 && $wp_query->max_num_pages == 1) { wp_redirect( get_permalink( $wp_query->posts[’0’]->ID ) ); exit; } } } add_action(‘template_redirect’, ‘redirect_single_post’); function redirect_single_post() { if (is_search()) { global $wp_query; if ($wp_query->post_count == […]

WordPress 用户注册后自动登录/首次登录跳转到指定页面

本文目录[隐藏]1自动登录并重定向2一定时间内登录重定向3一定时间内首次登录重定向(Cookie 版)4一定时间内首次登录重定向(字段 版) 对于开放注册的WordPress站点,用户登录后的页面跳转(重定向)是需要好好考虑的。之前分享过《WordPress 登录/登出(注销)后返回之前访问的页面》,今天要说的是将首次登录或者在指定时间内登录的用户,重定向到指定页面。以下代码都可以添加到主题的 functions.php 自动登录并重定向 注册后自动登录并且重定向到指定页面,其实也是实现首次登录重定向的最简便的方法。 1 2 3 4 5 6 7 8 9 10 11 12 13 /** * 用户注册成功后自动登录,并跳转到指定页面 * https://www.wpdaxue.com/user-first-login-redirect.html */ function auto_login_new_user( $user_id ) { // 用户注册后自动登录 wp_set_current_user($user_id); wp_set_auth_cookie($user_id); // 这里跳转到 http://域名/about 页面,请根据自己的需要修改 wp_redirect( home_url().’about’ ); exit; } add_action( ‘user_register’, ‘auto_login_new_user’); /** * 用户注册成功后自动登录,并跳转到指定页面 * https://www.wpdaxue.com/user-first-login-redirect.html */ function […]

100% Secure Checkout

PayPal / MasterCard / Visa