WordPress 如何通过 functions.php 排除某些类别

WordPress how to exclude certain categories via functions.php

我正在尝试排除某些类别和所有用户档案
例如

从我的 WordPress 站点通过 functions.php,将它们重新路由到主页。

这是我到目前为止尝试过的方法:

/* Route categories to home */
function reroute_wp_categories(){
  if( is_category( array('category1', 'category2', 'category3' )) || is_author() ) {
    global $wp_query;
    $wp_query->is_home(); //set to Home
  }
}

类别 x 是类别 slug。

这根本行不通,我运行没主意了。

感谢任何help/ideas。

这应该可以解决问题。在您的 functions.php 中添加以下内容

function reroute_wp_categories( $query ) {
    if ( is_author() || is_category(array('category1', 'category2', 'category3' )) ) {
        wp_redirect(home_url());
        exit();
    }
}
add_action( 'pre_get_posts', 'reroute_wp_categories' );