使用Timber和Twig在WordPress中处理重定向的最佳方式是什么



我想将作者、类别和标记页面重定向到我的主页。在Yoast SEO中,我已经将这些页面设置为不索引,但我希望它们能够永久重定向到主页。

我在functions.php中尝试了以下代码,但没有效果:

// Redirect all author archives to the website's about page
add_filter('author_link', 'ehi_author_link');
function ehi_author_link()
{
return home_url('home');
}

add_action('template_redirect', 'my_custom_disable_author_page');
function my_custom_disable_author_page() {
global $wp_query;
if ( is_author() ) {
// Redirect to homepage, set status to 301 permenant redirect.
// Function defaults to 302 temporary redirect.
wp_redirect(get_option('home'), 301);
exit;
}
}

你走对了。使用home_url将直接获得您的主页url。

function redirect_author() {
if ( is_author() || is_category() || is_tag() ) {
wp_redirect( home_url(), 301 ); 
exit;
}
}
add_action( 'template_redirect', 'redirect_author' );

最新更新