星期四 , 23 1 月 2025

从分类url中去除category方法二

// Remove 'category' base and parent slug from category URLs
function remove_category_from_url($string, $type) {
    // Remove '/category/' from the URL
    if ($type !== 'single') {
        $string = str_replace('/category/', '/', $string);
    }

    // If this is a child category, remove the parent category slug
    if (is_category() && $type !== 'single') {
        $category = get_queried_object();
        if ($category && $category->parent) {
            // If it's a child category, remove the parent category slug
            $parent_category = get_category($category->parent);
            $string = str_replace('/' . $parent_category->slug . '/', '/', $string);
        }
    }

    return $string;
}
add_filter('user_trailingslashit', 'remove_category_from_url', 10, 2);

// Add custom rewrite rules for categories (removes both '/category/' and parent slug)
function custom_category_rewrite_rules($wp_rewrite) {
    $new_rules = array();

    // Get all categories, including parent and child categories
    $categories = get_categories(array('hide_empty' => false, 'parent' => 0)); // Top-level categories

    // Rewrite rule for top-level categories (directly remove '/category/' base)
    foreach ($categories as $category) {
        $new_rules[$category->slug . '/?$'] = 'index.php?category_name=' . $category->slug;
        $new_rules[$category->slug . '/page/([0-9]{1,})/?$'] = 'index.php?category_name=' . $category->slug . '&paged=$matches[1]';

        // Get child categories for each top-level category
        $child_categories = get_categories(array('hide_empty' => false, 'parent' => $category->term_id));
        foreach ($child_categories as $child_category) {
            // Remove parent category slug and the '/category/' base
            $new_rules[$child_category->slug . '/?$'] = 'index.php?category_name=' . $child_category->slug;
            $new_rules[$child_category->slug . '/page/([0-9]{1,})/?$'] = 'index.php?category_name=' . $child_category->slug . '&paged=$matches[1]';
        }
    }

    // Merge new rules with existing ones
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'custom_category_rewrite_rules');

// Redirect old category URLs to the new ones (both parent and child categories)
function redirect_old_category_url() {
    // Check if 'category' base is in the URL
    if (strpos($_SERVER['REQUEST_URI'], '/category/') !== false) {
        wp_redirect(home_url(str_replace('/category', '', $_SERVER['REQUEST_URI'])), 301);
        exit;
    }
}
add_action('template_redirect', 'redirect_old_category_url');

// Modify admin category links to remove parent slug and '/category/' base
function remove_category_slug_from_admin_links($term_link, $term) {
    if ($term->taxonomy === 'category') {
        // Remove '/category/' base
        $term_link = str_replace('/category/', '/', $term_link);
        
        // If it's a child category, remove the parent category slug
        if ($term->parent) {
            $parent_category = get_category($term->parent);
            $term_link = str_replace('/' . $parent_category->slug . '/', '/', $term_link);
        }
    }
    return $term_link;
}
add_filter('get_term_link', 'remove_category_slug_from_admin_links', 10, 2);