首页 > 解决方案 > “无法重新声明 post_redirect_by_custom_filters()”的任何解决方案?

问题描述

我正在使用下面的代码将一个类别的所有帖子重定向到其他域

add_action('template_redirect', 'post_redirect_by_custom_filters');
function post_redirect_by_custom_filters() {
    global $post;
    // this array can contain category names, slugs or even IDs.
    $catArray = ['HW', '123x', '11x', '111x', '1111x'];
    if (is_single($post->ID) && has_category($catArray, $post)) {
        $new_url = "https://my2nddomain.com/{$post->post_name}/";  
        wp_redirect($new_url, 301);
        exit;
    }
}

但现在我想将某些类别的帖子重定向到我的第三个站点。但是当我在 funtion.php 中重用此代码时,我收到此错误 Cannot redeclare post_redirect_by_custom_filters() (之前在 wp-content/themes/poster/functions.php 中声明)

有什么解决方案吗?

标签: wordpressredirect

解决方案


推荐阅读