好吧,你可以把代码从链接的问题和解决的问题转180度。
只是开玩笑。
我将从获取当前页面的slug开始,使用该slug搜索包含该slug的类别,如果找到,则重定向到该类别
add_action(\'template_redirect\', \'bt_redirect_page_to_category\');
function bt_redirect_page_to_category () {
// if we are not in page, no need to run the code
if (!is_page()) return;
// get current page object
global $post;
// get page slug
$slug = $post->post_name;
// the term taxonomy
$taxonomy = \'category\';
// get category
// you can use get_category_by_slug(), but I prefer get_term_by()
$category = get_term_by(\'slug\', $slug, $taxonomy);
// if no category found, no need to continue with the code
if (empty($category)) return;
// found category so we can redirect
if ( wp_safe_redirect( get_term_link( $category, $taxonomy ), 301 ) ) {
exit;
}
}