if (is_author(\'admin\')){
add_action(\'template_redirect\', \'wpse_redirect_posts_to_new_page\');
function wpse_redirect_posts_to_new_page() {
// if this is not a single post or not of type post, do nothing
if ( ! is_single() || get_post_type() != \'post\')
return;
$url = parse_url(get_the_permalink());
$newdomain = \'https://www.example.com\';
wp_redirect($newdomain . $url[\'path\']);
exit;
}
}
目前,它正在将所有用户的帖子重定向到新的url将特定作者帖子重定向到另一个URL
1 个回复
SO网友:Krzysiek Dróżdż
首先[is_author][1]
不检查给定用户是否是当前帖子的作者。直接来自Codex:
此条件标记检查是否正在显示作者存档页。这是一个布尔函数,意味着它返回TRUE或FALSE。
所以这对你没有帮助。
代码的另一个问题是,您提前执行此检查。如果粘贴此代码functions.php
, 然后在解析查询之前检查if条件。
那么它到底应该是什么样子呢?类似这样:
function wpse_redirect_posts_to_new_page() {
// if this is not a single post or not of type post, do nothing
if ( ! is_single() || \'post\' != get_post_type() ) {
return;
}
$post = get_queried_object(); // it\'s single, so this will be a post
if ( AUTHOR_ID != $post->post_author ) { // change AUTHOR_ID to real ID of the author
return;
}
$url = parse_url(get_the_permalink());
$newdomain = \'https://www.example.com\';
wp_redirect($newdomain . $url[\'path\']);
exit;
}
add_action(\'template_redirect\', \'wpse_redirect_posts_to_new_page\');
结束