笔记
- 您应该在;之后添加
exit()
或die()
wp_redirect()
wp
改为在init
. 这将确保您已经加载了模板。
- 如果模板文件在子目录下,那么您必须检查该部分。例如:
/wp-content/themes/my_active_theme/page-templates/list-projects.php
,那么你必须检查page-templates/list-projects.php
以下是适合您的代码:
function wh_redirect_sample()
{
if (basename(get_page_template()) == 'list-projects.php')
{
wp_redirect('http://url.com.au/profile');
exit(); //always remember to add this after wp_redirect()
}
}
add_action('wp', 'wh_redirect_sample');
替代方法:
function wh_redirect_sample()
{
//if list-projects.php is under sub directory say /wp-content/themes/my_active_theme/page-templates/list-projects.php
if (is_page_template('page-templates/list-projects.php'))
{
wp_redirect('http://url.com.au/profile');
exit();
}
//if list-projects.php is under active theme directory say /wp-content/themes/my_active_theme/list-projects.php
if (is_page_template('list-projects.php'))
{
wp_redirect('http://url.com.au/profile');
exit();
}
}
add_action('wp', 'wh_redirect_sample');
代码进入活动子主题(或主题)的 function.php 文件中。或者也可以在任何插件 php 文件中。
代码经过测试并且可以工作。
希望这可以帮助!